簡體   English   中英

嘗試通過Node.js使用Firebase發送通知時出現ReferenceError

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

所以我想通過Firebase上的功能發送通知。 我正在通過Node.js在JavaScript上進行通知編程。 在從一個帳戶單擊“ Send Friend Request后,另一個人應該收到我在下面附加的JavaScript文件的有效負載上指定的通知。 我的Firebase函數上不斷出現以下錯誤

ReferenceError:事件未定義。

這是確切錯誤的圖像

這是我的JavaScript文件:

/*
 * 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');

  });


  });





});

這是JavaScript文件中引用的Firebase數據庫的父母和子女的照片

由於錯誤指出未定義事件,因此我試圖找出未定義的事件。

這是什么問題?

您尚未在此代碼塊中定義事件:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM