0
votes

I am currently developing appointment application and notification is one of the application. As a result, I have been using firebase function for notifying a user if there is new appointment booked or cancel. I am getting an error that 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');

    });


  });


});

Error log 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) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20) at /var/tmp/worker/worker.js:733:24 at process._tickDomainCallback (internal/process/next_tick.js:135:7) **Database structure ** enter image description here

1

1 Answers

0
votes

The error message is very precise. It's telling you that you used a variable called event on line 16, but you never defined it:

if(!event.data.val())