【问题标题】:How to fix Cloud Function error admin.database.ref is not a function at exports如何修复 Cloud Function 错误 admin.database.ref 不是导出时的函数
【发布时间】:2019-03-26 13:41:24
【问题描述】:

我目前正在尝试修改我的 Cloud Functions 并移入 https.onRequest 下,以便我可以调用使用它来安排 cron 作业。我如何在日志中收到以下错误。

TypeError: admin.database.ref 不是函数 在exports.scheduleSendNotificationMessageJob.functions.https.onRequest (/user_code/index.js:30:20) 在 cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)

exports.scheduleSendNotificationMessageJob = functions.https.onRequest((req, res) => {
    admin.database.ref('/notifications/{studentId}/notifications/{notificationCode}')
        .onCreate((dataSnapshot, context) => {
            const dbPath = '/notifications/' + context.params.pHumanId + '/fcmCode';
            const promise = admin.database().ref(dbPath).once('value').then(function(tokenSnapshot) {
                const theToken = tokenSnapshot.val();
                res.status(200).send(theToken);
                const notificationCode = context.params.pNotificationCode;
                const messageData = {notificationCode: notificationCode};
                const theMessage = {    data: messageData,
                    notification: { title: 'You have a new job reminder' }
                };
                const options = {   contentAvailable: true,
                    collapseKey: notificationCode };
                const notificationPath = '/notifications/' + context.params.pHumanId + '/notifications/' + notificationCode;
                admin.database().ref(notificationPath).remove();
                return admin.messaging().sendToDevice(theToken, theMessage, options);
            });
            return null;
        });
});

【问题讨论】:

    标签: javascript node.js firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    您不能在 HTTP 云函数的定义中使用onCreate() 实时数据库触发器的定义。

    如果您切换到 HTTP 云函数“以便(您)可以调用使用它来安排 cron 作业”,则意味着触发器将是对 HTTP 云函数的调用。换句话说,当在实时数据库中创建新数据时,您将无法再触发操作(或云函数)。

    你可以很好的做的是读取实时数据库的数据,例如如下(发送通知的简化场景):

    exports.scheduleSendNotificationMessageJob = functions.https.onRequest((req, res) => {
    
        //get the desired values from the request
        const studentId = req.body.studentId;
        const notificationCode = req.body.notificationCode;
    
        //Read data with the once() method
        admin.database.ref('/notifications/' + studentId + '/notifications/' + notificationCode)
         .once('value')
         .then(snapshot => {
             //Here just an example on how you would get the desired values
             //for your notification
             const theToken = snapshot.val();
             const theMessage = ....
             //......
    
             // return the promise returned by the sendToDevice() asynchronous task
             return admin.messaging().sendToDevice(theToken, theMessage, options)
          })
          .then(() => {
             //And then send back the result (see video referred to below)
             res.send("{ result : 'message sent'}") ;
          })
          .catch(err => {
             //........
          });
    
    });
    

    您可以观看以下有关 HTTP 云函数的官方 Firebase 视频:https://www.youtube.com/watch?v=7IkUgCLr5oA&t=1s&list=PLl-K7zZEsYLkPZHe41m4jfAxUi0JjLgSM&index=3。它显示了如何从 Firestore 读取数据,但读取和发送回响应(或错误)的概念与实时数据库相同。与该系列的另外 2 个视频 (https://firebase.google.com/docs/functions/video-series/?authuser=0) 一起,它还解释了正确链接 Promise 以及向平台指示 Cloud Function 工作已完成的重要性。

    【讨论】:

      【解决方案2】:

      对我来说,这个错误发生在写admin.database而不是admin.database()时。

      【讨论】:

        猜你喜欢
        • 2019-10-30
        • 2019-12-25
        • 2021-06-12
        • 2019-07-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-26
        • 2019-07-10
        • 2020-07-25
        相关资源
        最近更新 更多