Firebase 函数:类型错误:无法读取未定义的属性'user_id'



我想在android上构建一个聊天平台作为Labit Chat我正在编写这个node.js脚本并部署它:

'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((change, context) => {

const user_id = change.params.user_id;
const notification_id = change.params.notification_id;
console.log('We have a notification from : ', user_id);

if (!change.after.val()) {
return console.log('A notification has been deleted from database: ', notification_id);
}

const fromUser = admin.database().ref(`/notifications/${user_id}/${notification_id}`).once('value');
return fromUser.then(fromUserResult => {
const from_user_id = fromUserResult.val().from;
console.log('You have new notification from  : ', from_user_id);

const userQuery = admin.database().ref(`Users/${from_user_id}/name`).once('value');
const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');
return Promise.all([userQuery, deviceToken]).then(result => {
const userName = result[0].val();
const token_id = result[1].val();

const payload = {
notification: {
title : "New Friend Request",
body: `${userName} has sent you request`,
icon: "default",
click_action : "com.example.ragha.safeheartchatting_TARGET_NOTIFICATION"
},
data : {
from_user_id : from_user_id
}
};

return admin.messaging().sendToDevice(token_id, payload).then(response => {
return console.log('This was the notification Feature');
});
});
});
});

在运行应用程序并在firebase云上发送请求后,我得到了一个错误,它出现在firebase数据库中,但没有出现在移动android 中

TypeError:无法读取未定义的属性"user_id"位于exports.sendNotification.functions.database.ref.onWrite.event(/user_code/index.js:12:31(在cloudFunctionNewSignature(/user_code/node_modulesfirebase-functions/lib/cloud-functions.js:105:23(at cloudFunction(/user_code/node_modulesfirebase-functions/lib/cloud-functions.js:135:20(网址:/var/tmp/worker/worker.js:768:24正在处理中_tickDomainCallback(internal/process/next_tick.js:135:7(

如果想要通配符的值,请使用context,而不是changechange只是描述了数据库中触发对函数调用的更改。

const user_id = context.params.user_id;
const notification_id = context.params.notification_id;

您应该阅读数据库触发器的文档来学习API。

相关内容

  • 没有找到相关文章

最新更新