Javascript Firebase云消息传递:未定义事件
Firebase云消息通知有问题。当我想发送好友请求时,另一个客户端没有收到通知。Firebase功能日志显示:Javascript Firebase云消息传递:未定义事件,javascript,firebase,firebase-cloud-messaging,google-cloud-functions,Javascript,Firebase,Firebase Cloud Messaging,Google Cloud Functions,Firebase云消息通知有问题。当我想发送好友请求时,另一个客户端没有收到通知。Firebase功能日志显示: ReferenceError: event is not defined at exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:14:8) at cloudFunctionNewSignature (/user_code/node_modules/firebase
ReferenceError: event is not defined
at exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:14:8)
at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:109:23)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:139:20)
at /var/tmp/worker/worker.js:728:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
下面是JavaScript代码:
'use strict'
const functions = require('firebase-functions');
const admin = require ('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/Notifications/{receiver_id}/{notification_id}').onWrite((change,context) => {
const receiver_id = context.params.receiver_id;
const notification_id = context.params.notification_id;
console.log('We have a notification to send to: ', receiver_id);
if (!event.data.val) {
return console.log('A notification has been deleted from database: ', notification_id);
}
const deviceToken = admin.database().ref(`/Users/${receiver_id}/device_token`).once('value');
return deviceToken.then(result => {
const token_id = result.val();
const payload = {
notification:
{
title: "Friend Request",
body: "you have received a new friend request",
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response => {
console.log('This was the notification feature.');
});
});
});
更改此项:
if (!event.data.val) {
return console.log('A notification has been deleted from database: ', notification_id);
}
为此:
if (!change.after.val()) {
return console.log('A notification has been deleted from database: ', notification_id);
}
该对象有两个属性,每个属性都是一个DataSnapshot
,方法与中的相同
也是一种方法而不是属性。此处未定义此
事件,即事件.data.val
。在您的上限范围中甚至没有对事件的引用。您希望事件是什么?你期望它从哪里来?