簡體   English   中英

Firebase功能參考錯誤

[英]Firebase Functions Reference Error

我正在使用Firebase函數向每個用戶發起通知推送。 該函數如下所示:

  'use strict'

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

exports.sendNotification = functions.database.ref('/Notifications/{recieverid}/{notificationid}')
    .onWrite((data,context) =>
    {
        console.log(data);
    const recieverid = context.params.recieverid;
    const notificationid = context.params.notificationid;


    const deviceToken = admin.database().ref(`/users/${recieverid}/token`).value;

    return deviceToken.then(result=> 
    {
        const token = result.val();
        const payload = 
        {
            notification: 
            {
                title: "You've been caught!",
                body: "Somebody likes you as much as you like them",
                icon:"default"
            }
        };
      return admin.messaging().sendToDevice(token, payload);

    });
});

並且我通過日志收到以下錯誤:

TypeError: Cannot read property 'then' of undefined
    at exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:17:20)
    at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
    at next (native)
    at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
    at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
    at /var/tmp/worker/worker.js:728:24
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

關於為什么我得到上述錯誤,更重要的是如何解決它的任何想法? 是JS的新功能,但不是Java的功能。

詹姆士

const reciever_id = event.params.reciever_id;
const notification_id = event.params.notification_id;
console.log('We have a notification to send to: ', reciever_id);

if(!event.data.val());

錯誤是因為未聲明名為event變量。 也許您將使用datacontext而不是event因為函數在onWrite它們onWrite

找到了答案,代碼如下:

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

exports.sendNotification = functions.database
.ref('/notifications/{recieverid}/{notificationid}')
.onWrite((snapshot,context) => {
    console.log(snapshot);
    const recieverid = context.params.recieverid;
    const notificationid = context.params.notificationid;

    return admin.database().ref(`/users/${recieverid}/token`).once('value', function(snapshot) {
        const token = snapshot.val();
        const payload = 
        {
            notification: 
            {
                title: "SomeTitle",
                body: "SomeBody",
                icon: "default"
            }
        };
        return admin.messaging().sendToDevice(token, payload).then(function(response){
            return console.log("Successfully sent message:", response)
        });
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM