Node.js无法读取属性"user_id"



我是安卓系统的新手,我正在处理通知,但我尝试了所有与我的链接相关的链接,仍然收到了相同的错误。如有任何帮助,我们将不胜感激。预付款谢谢

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)

您可能正在使用1.0版以下的Firebase SDK for Cloud Functions。

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

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

相关内容

  • 没有找到相关文章

最新更新