【发布时间】:2019-09-13 07:13:36
【问题描述】:
我有一个 Android 应用程序,希望人们(经过身份验证的用户)通过消息框互相发送推送通知。我正在使用带有 firebase 云功能的 node.js,我在日志中得到了这个 error:
TypeError:无法读取未定义的属性“userId” 在exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:10:33) ...
消息已成功写入实时数据库,但通知未传递给接收者(用户)。
我阅读了很多文档和相同/类似的问题,所以我知道有很多与此相关的主题,但我无法解决问题。我使用index.js作为源代码,但根据我阅读的文档更改了一些部分,如onWrite部分。
错误表示以下代码中的这一行:
const receiverId = context.params.userId;
params 出了问题。这是代码的一小部分(我的更改):
let functions = require('firebase-functions');
let admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database.ref('/messages/{userId}/{messageId}').onWrite((change,context) => {
//get the userId of the person receiving the notification because we need to get their token
const receiverId = context.params.userId;
console.log("receiverId: ", receiverId);
//get the user id of the person who sent the message
const senderId = change.child('user_id').val();
console.log("senderId: ", senderId);
//get the message
const message = change.child('message').val();
console.log("message: ", message);
//get the message id. We'll be sending this in the payload
const messageId = context.params.messageId;
console.log("messageId: ", messageId);
...
【问题讨论】:
标签: node.js firebase-realtime-database firebase-cloud-messaging google-cloud-functions