这是错误;
TypeError:无法读取未定义的属性“ val” 在exports.sendNotification.functions.database.ref.onWrite(/user_code/index.js:14:19) 在对象。 (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) 在下(本机) 在/user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 在__awaiter(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) 在cloudFunction(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) 在/var/tmp/worker/worker.js:728:24 在process._tickDomainCallback(internal / process / next_tick.js:135:7)
这是我的代码:
exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((change, context) => {
const user_id = context.params.user_id;
const notification = context.params.notification;
console.log('We have a notification to send to : ', user_id);
if(!context.data.val()){
return console.log('A notification has been deleted from the database:', notification_id);
}
const deviceToken = admin.database().ref(`/Users/${user_id}/deviceToken`).once('value');
return deviceToken.then(result => {
const token_id = result.val();
const payload = {
notification: {
title : "Friend Request",
body : "You've receieved a new Friend Request",
icon : "default"
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response => {
console.log('This is the notification Feature');
});
});
});
答案 0 :(得分:1)
使用apr-util
代替context.data.val()
请测试此功能:
change.after.exists()
答案 1 :(得分:0)
代替
const token_id = result.val();
尝试
const token_id = result.after.data();