简体   繁体   中英

ReferenceError when trying to send notification with Firebase via Node.js

So I am trying to send a notification via Functions on Firebase. I am doing the notification programming on JavaScript via Node.js. Upon clicking Send Friend Request from one account, the other person is suppose to get a notification as specified on the payload of the JavaScript file I have attached below. I keep getting the following error on my Firebase Functions

ReferenceError: event is not defined.

Here is an image of the exact error.

Here is my JavaScript file:

/*
 * Functions SDK : is required to work with firebase functions.
 * Admin SDK : is required to send Notification using functions.
 */

//This runs JavaScript in Strict Mode, which prevents the use of things such as undefined variables.
'use strict'

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);


/*
 * 'OnWrite' works as 'addValueEventListener' for android. It will fire the function
 * everytime there is some item added, removed or changed from the provided 'database.ref'
 * 'sendNotification' is the name of the function, which can be changed according to
 * your requirement
 */

exports.sendNotification = functions.database.ref('/Notifications/{retrieveUserId}/{notificationId}').onWrite((data, context) => {


  /*
   * You can store values as variables from the 'database.ref'
   * Just like here, I've done for 'user_id' and 'notification'
   */

  const retrieveUserId = context.params.retrieveUserId;
  const notificationId = context.params.notificationId;

  console.log('User id is : ', retrieveUserId);

  //Prevents notification being sent if there are no logs of notifications in the database.

  if (!event.data.val()) {

    return console.log('A notification has been deleted from the database : ', notificationId);

  }

  const deviceToken = admin.database().ref(`/Users/${retrieveUserId}/device_token`).once('value');

  return deviceToken.then(result => {

    const tokenId = result.val();


    const payload = {
    notification: {
        title: "Friend Request",
        body: "You have received a friend request from Slim Shady",
        icon: "default"
    }
  };

  return admin.messaging().sendToDevice(tokenId, payload).then(response => {

    return console.log('Notification was sent to the user');

  });


  });





});

This is a picture of parents and children of my Firebase database referred to in the JavaScript file.

As the error states an event not being defined, I'm trying to figure out which event I have not defined.

What is the issue here?

您尚未在此代码块中定义事件:

if (!event.data.val()) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM