【问题标题】:node.js Cannot read property 'user_id'node.js 无法读取属性'user_id'
【发布时间】:2018-11-01 22:38:28
【问题描述】:

我是 Android 新手,我正在使用通知,但我尝试了所有与我相关的链接,但仍然遇到同样的错误。任何帮助,将不胜感激。提前谢谢

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)

【问题讨论】:

    标签: node.js firebase npm google-cloud-functions firebase-notifications


    【解决方案1】:

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

    对于 SDK 版本 event.params.user_id 将引发错误。事实上,Firebase SDK for Cloud Functions 1.0.0 版在 API 中引入了一些重要更改,请参阅https://firebase.google.com/docs/functions/beta-v1-diff#realtime-database

    您应该在您的package.json 文件中验证您使用的是哪个版本的firebase-functions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      • 2020-02-16
      • 1970-01-01
      相关资源
      最近更新 更多