【问题标题】:Error 'myFunctions.keepTimesUpdated is not a function' in my Cloud Function我的云函数中出现错误“myFunctions.keepTimesUpdated 不是函数”
【发布时间】:2017-03-23 18:49:35
【问题描述】:

我是 JavaScript 新手,我正在尝试在部署 Firebase Cloud Function 之前对其进行测试,但测试失败,我似乎无法弄清楚。当我运行我的 mocha 测试时,我得到了......

TypeError: myFunctions.keepTimesUpdated is not a function
  at Context.it (testing.js:45:32)

我真的不知道为什么。

这是我的 testing.js

const chai = require('chai');

const assert = chai.assert;

const chaiAsPromised = require('chai-as-promised');

chai.use(chaiAsPromised);

const sinon = require('sinon');


describe('Cloud Functions', () => {
    var myFunctions, configStub, adminInitStub, functions, admin;

    before(() => {
        admin = require('firebase-admin');
        adminInitStub = sinon.stub(admin, 'initializeApp');

        functions = require('firebase-functions');
        configStub = sinon.stub(functions, 'config').returns({
            firebase: {
                databaseURL: 'https://hallpass-v2.firebaseio.com',
                storageBucket: 'not-a-project.appspot.com',

            }
        });

        myFunctions = require('/Users/mitchellgant/Documents/OldDesktop/XCode/HallPass-v2/functions/index')
    });

    after(() =>  {
        configStub.restore();
        adminInitStub.restore();

    });

    describe('keepTimesUpdated', () => {

        it('should update the nextWeek val', () => {

            const fakeEvent = {
                data: new functions.database.DeltaSnapshot(null, null, null, null,'timeInfo/currentDate')
            };

            return myFunctions.keepTimesUpdated(fakeEvent);


        })



    });

})

这是我的 index.js

var functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);


// // Start writing Firebase Functions
// // https://firebase.google.com/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// })

let keepTimesUpdated = functions.database.ref('timeInfo/currentDate').onWrite(async event => {
    let snap = event.data;
    let curDate = snap.value;
    let nextWeekRef = snap.ref.parent.child('nextWeek');
    let nextWeek = nextWeekRef.value;
    let nextMonthRef = snap.ref.parent.child('nextMonth')
    let nextMonth = nextMonthRef.value
    let date = new Date();
    let curMonth = date.getMonth() + 1;
    // if (curDate >= nextWeek) {
    //     nextWeekRef.set(nextWeek + 604800000);
    //     snap.ref.parent.parent.child('Schools/{school}/teachers/{teacher}').child('needToUpdateWeekVals').set(true);
    // }
    // if ( curMonth >= nextMonth) {
    //     nextMonthRef.set(nextMonth + 1);
    //     snap.ref.parent.parent.child('Schools/{school}/teachers/{teacher}').child('needToUpdateMonthVals').set(true);
    // }
})

任何见解都会非常有帮助。

【问题讨论】:

    标签: javascript firebase google-cloud-functions


    【解决方案1】:

    你需要导出你的函数:把let keepTimesUpdated = ...改成exports.keepTimesUpdated = ...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-23
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多