我正在使用Notifications,但是Notification无法正常工作并出现此错误,对此我是陌生的,所以我无法完全理解此问题并因此而被错误所困扰。任何帮助,我们将不胜感激,
ReferenceError: event is not defined
at exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:9:31)
at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)
at /var/tmp/worker/worker.js:744:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
这是我的index.js
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 = event.params.user_id;
const notification = event.params.notification;
console.log('We have a notification to send to: ', user_id);
if(!event.data.val())
{
return console.log('A Notification has been deleted from database: ', notification_id);
}
const fromUser = admin.database().ref(`/notifications/${user_id}/${notification_id}`);
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: "Message Request",
body: `${userName} has sent you a Message Request`,
icon: "logo.png",
click_action: "com.example.gab.quadrantms_TARGET_NOTIFICATION"
},
data : {
from_user_id : from_user_id
}
};
return admin.messaging().sendtoDevice(token_id, payload).then(response =>{
console.log('This was the Notification Feature');
return true;
});
});
});
});
答案 0 :(得分:1)
正如错误所言,event
在任何地方都没有定义。看来您应该改用context
。
const user_id = context.params.user_id;
const notification = context.params.notification;
以下是EventContext
的文档:https://firebase.google.com/docs/reference/functions/functions.EventContext
和RefBuilder.onWrite
:https://firebase.google.com/docs/reference/functions/functions.database.RefBuilder#onWrite