首页 > 解决方案 > 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) **数据库结构** 在此处输入图像描述

标签: javascriptnode.jsfirebase-realtime-databasefirebase-cloud-messaginggoogle-cloud-functions

解决方案


错误信息非常准确。它告诉你你使用了一个event在第 16 行调用的变量,但你从未定义它:

if(!event.data.val())

推荐阅读