【发布时间】:2018-04-07 14:00:39
【问题描述】:
我想通过 Firebase 云功能发送设备到设备的推送通知。我正在编写 node.js 脚本
exports.sendNotification = functions.database.ref('/messages/{user_id}/{rec_user_id}').onWrite((event) => {
const sender_user_id = event.params.user_id;
const receiver_user_id = event.params.rec_user_id;
console.log('We have a notification from : '+ sender_user_id+ " | Receiced by " + receiver_user_id);
return;
});
在 Firebase 云上部署功能后出现错误
TypeError: Cannot read property 'user_id' of undefined
完整的错误日志是:
TypeError: Cannot read property 'user_id' of undefined
at exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:28:40)
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:700:26
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
我得到了here的答复
它工作正常,但是当我使用时
if(!event.data.val()){
return console.log('A Notification has been deleted from the database : ', sender_user_id);
}
Firebase 日志再次开始给我一条错误消息说
Cannot read property 'val' of undefined
如何摆脱这些错误? 提前致谢
【问题讨论】:
-
我收到错误无法读取属性'val',如何解决这个问题??
标签: javascript node.js firebase firebase-realtime-database google-cloud-functions