我正在编写一个程序,以便从firebase的Notifications节点内部获取当前用户ID的值。从那里,我想获取其ID存储在Notification节点中的该用户的详细信息。但是我遇到了cannot read property of curr_uid of undefined
这是我的index.js文件
const functions = require('firebase-functions');
const admin = require ('firebase-admin');
admin.initializeApp (functions.config().firebase);
exports.sendNotification = functions.database.ref('/Notifications/{curr_uid}').onWrite(event =>
{
const curr_uid = event.params.curr_uid;
console.log ('uid',curr_uid);
return admin.database().ref('Notifications/{curr_uid}').once('value').then(snapshot =>
{
if(snapshot.exists())
{
const sender_uid = snapshot.val().By;
console.log('New notification from ',sender_uid);
}
});
const payload = {
notification: {
title: "New Post",
body: "A new post is added",
icon: "default"
}
};
return admin.database().ref('Tokens').once('value').then(allToken =>
{
if(allToken.val())
{
console.log('token available');
const token = Object.keys(allToken.val());
return admin.messaging().sendToDevice(token,payload);
}
else
{
console.log('No token available');
}
});
});
当将当前用户ID子节点添加到该curr_uid
的Notifications节点时,我希望向所有设备发送推送通知。我想在该curr_uid
节点中获取“按”子键和值,以便以后可以使用它通过用户名发送通知。