javascript - ReferenceError:事件未定义firebase函数
问题描述
我目前正在开发预约应用程序,通知是应用程序之一。因此,我一直在使用 firebase 功能来通知用户是否有新的约会预订或取消。我收到一个错误,即 ReferenceError: event is not defined Node.js code
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/Notifications/{PostKey}/{notification_id}').onWrite((data, context) => {
const user_id = context.params.PostKey;
const notification_id = context.params.notification_id;
console.log('We have a notification from : ', user_id,'this also notification_id',notification_id);
if(!event.data.val())
{
return console.log('A Notification has been deleted from the database : ', notification_id);
}
const deviceToken = admin.database().ref(`/User_Data/${user_id}/Device_Token`).once('value');
console.log('A Notification has been deleted from the database : ', deviceToken);
return deviceToken.then(result => {
const token_id = result.val();
const payload =
{
notification:
{
title:"Appointment has been booked",
body: "one of your Appointmtnt has been booked",
icon:"default"
}
};
return admin.messaging().sendToDevice(token_id,payload).then(response =>
{
return console.log('This was the notification Feature');
});
});
});
错误日志 ReferenceError: event is not defined at exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:16:9) at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions. js:105:23) 在 cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20) 在 /var/tmp/worker/worker.js:733:24 在 process._tickDomainCallback ( internal/process/next_tick.js:135:7) **数据库结构**
解决方案
错误信息非常准确。它告诉你你使用了一个event
在第 16 行调用的变量,但你从未定义它:
if(!event.data.val())
推荐阅读
- python - 有没有办法找出特定事件的时间总和?
- java - Java util Logger不写入文件
- regex - 使用 Powershell 从字符串中获取键值
- c++ - 使用-lstdc++构建cpp文件时如何知道使用哪个c++版本
- intellij-idea - java.lang.ClassNotFoundException:在 IntelliJ 中运行 Recorder.scala 时的记录器
- sql-server - 如何查询存储过程到父表和子表?
- python - 如何在 if else 语句中比较 request.get 结果
- oracle - 从两个表中选择数据得到 NULL
- node.js - 这个包是否在 npm 上正确注册?
- html - 创建带有水平和垂直线的树视图,显示使用 css 的连接性