簡體   English   中英

node.js無法讀取屬性“ user_id”

[英]node.js Cannot read property 'user_id'

我是Android的新手,正在使用Notifications,但我嘗試了與我的所有鏈接,但仍然遇到相同的錯誤。 任何幫助,將不勝感激。 提前謝謝

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

exports.sendNotification = 
functions.database.ref('/notifications/{user_id}/{notification_id}')
.onWrite((change, event) => {

    const user_id = event.params.user_id;
    const notification = event.params.notification;

    console.log('We have a notification to send to: ', user_id);

    if(!event.data.val())
    {
        return console.log('A Notification has been deleted from database: ', notification_id);
    }

    const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

    return deviceToken.then(result => {

        const token_id = result.val();

            const payload = {
              notification: {
                title: "Message Request",
                body: "You've received a Message Request",
                icon: "logo.png"
            }
        };

        return admin.messaging().sendtoDevice(token_id, payload).then(response =>{

            console.log('This was the Notification Feature');
            return true;
        });
    });
});

我得到了這個一致的錯誤。

TypeError: Cannot read property 'user_id' of undefined
at exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:9:31)at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)
at /var/tmp/worker/worker.js:744:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

您可能正在使用v1.0下的Firebase SDK for Cloud Functions版本。

對於版本低於1.0的SDK, event.params.user_id將引發錯誤。 實際上,針對雲功能的Firebase SDK 1.0.0版在API中引入了一些重要更改,請參閱https://firebase.google.com/docs/functions/beta-v1-diff#realtime-database

您應該在package.json文件中驗證使用的是哪個版本的firebase-functions

暫無
暫無

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

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