首页 > 解决方案 > 在函数日志中使用 firebase 处理通知时出现错误,我该如何解决?

问题描述

我正在使用颤振构建一个移动应用程序,我想让它支持自动推送通知,然后我使用了 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.jsgoogle-cloud-firestorefirebase-cloud-messaginggoogle-cloud-functions

解决方案


改变这个:

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

进入这个:

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

来自文档:

collection(collectionPath)

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


推荐阅读