参考错误事件未定义node.js [重复]
Posted
技术标签:
【中文标题】参考错误事件未定义node.js [重复]【英文标题】:reference error event is not defined node.js [duplicate] 【发布时间】:2019-04-07 00:07:40 【问题描述】:我正在使用通知,但通知不起作用并出现此错误,我对此并不陌生,因此我无法完全理解这一点并因错误而倾斜。任何帮助将不胜感激,我没有找到任何相关的解决方案
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;
);
);
);
);
【问题讨论】:
检查这个问题 - ***.com/questions/53006628/… 您的代码使用 Cloud Functions 的 1.0 之前的语法。请在此处学习迁移指南:firebase.google.com/docs/functions/beta-v1-diff,特别是 firebase.google.com/docs/functions/… 部分 还看到了这些上一个问题:***.com/a/49647257,***.com/a/52213392,***.com/a/52058241,***.com/a/50660963 span> 对不起,先生,我完全是初学者,所以我很难理解它 【参考方案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
【讨论】:
那么我应该将所有事件都更改为上下文吗? 或者您可以更改参数名称.onWrite((change, event) =>
以上是关于参考错误事件未定义node.js [重复]的主要内容,如果未能解决你的问题,请参考以下文章
使用 Node.js、Mongoose 和 Discord.js 的未定义错误 [无法读取未定义的属性]