【问题标题】:I got an error when I deal with notification using firebase in functions log, How can I fix it?在函数日志中使用 firebase 处理通知时出现错误,我该如何解决?
【发布时间】:2019-03-31 10:52:32
【问题描述】:

我正在使用 Flutter 构建一个移动应用程序,我想让它支持自动推送通知,然后我使用了 Firebase 云消息传递。 我写的函数:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
//admin.initializeApp();
var msgData;
exports.offerTrigger = functions.firestore.document('requests/{requestId}'
).onCreate((snapshot,context) => {
    msgData = snapshot.data();
    admin.firestore().Collection('pushtokens').get().then((snapshots) => {
        var tokens = [];
        if(snapshots.empty){
            console.log('No devices');
        }
        else {
            for(var token of snapshots.docs){
                tokens.push(token.data().deviceId);
            }
            var payload = {
                "notification" : {
                    "title" : "From" + msgData.Name,
                    "body"  : "Request " + msgData.requestDetial,
                    "sound" : "default"
                },
                "data" : {
                    "SenderName" : msgData.Name,
                    "message"    : msgData.requestDetial
                }
            }
            admin.messaging().sendToDevice(tokens,payload).then((respone) => {
                console.log("pushed them all");
            }).catch((err) => {
                console.log(err);
            });
        }
    })
})

在我部署我的函数后,当我添加一些文档时,我在 firebase 函数日志中遇到错误 错误:

TypeError: admin.firestore(...).Collection 不是函数

在exports.offerTrigger.functions.firestore.document.onCreate (/user_code/index.js:9:23)

在 cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:114:23)

在 cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:144:20)

在 /var/tmp/worker/worker.js:827:24

在 process._tickDomainCallback (internal/process/next_tick.js:135:7)

如何解决此错误?

【问题讨论】:

    标签: node.js google-cloud-firestore firebase-cloud-messaging google-cloud-functions


    【解决方案1】:

    改变这个:

        admin.firestore().Collection('pushtokens').get().then((snapshots) => {
    

    进入这个:

       admin.firestore().collection('pushtokens').get().then((snapshots) => {
    

    来自文档:

    collection(collectionPath)

    获取引用指定路径的集合的CollectionReference 实例。

    【讨论】:

    • 同一个兄弟 admin.firestore().Collection('pushtokens').get().then((snapshots) => { 进入这个:admin.firestore().collection('pushtokens' ).get().then((快照) => {
    • 应该是小写的,没有大写的Collection()函数,改成小写重新部署
    • 天哪,非常感谢兄弟,但现在我收到了新的错误返回未定义、预期的 Promise 或值,但我在我的应用程序中收到了通知
    • 在此处添加return return admin.messaging().sendToDevice(tokens,payload).then((respone)。请将我的答案(通过单击旁边的勾选标记)标记为正确,因为它对您有帮助
    • 衷心感谢兄弟愿上帝保佑你
    猜你喜欢
    • 2020-08-11
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 2023-02-08
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    相关资源
    最近更新 更多