【问题标题】:ReferenceError: event is not defined firebase functionReferenceError:事件未定义firebase函数
【发布时间】:2019-02-26 03:20:12
【问题描述】:

我目前正在开发约会应用程序,通知是应用程序之一。因此,我一直在使用 firebase 功能来通知用户是否有新的约会预订或取消。我收到一个错误,即 ReferenceError: event is not defined Node.js 代码

    '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:未定义事件 在exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:16:9) 在 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) **数据库结构**

【问题讨论】:

    标签: javascript node.js firebase-realtime-database firebase-cloud-messaging google-cloud-functions


    【解决方案1】:

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

    if(!event.data.val())
    

    【讨论】:

      猜你喜欢
      • 2019-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      相关资源
      最近更新 更多