【问题标题】:ReferenceError when trying to send notification with Firebase via Node.js尝试通过 Node.js 使用 Firebase 发送通知时出现 ReferenceError
【发布时间】:2018-09-14 11:07:36
【问题描述】:

所以我正在尝试通过 Firebase 上的功能发送通知。 我正在通过 Node.js 在 JavaScript 上进行通知编程。 从一个帐户单击Send Friend Request 后,假设另一个人会收到我在下面附加的 JavaScript 文件的有效负载上指定的通知。 我的 Firebase 函数不断出现以下错误

ReferenceError: 事件未定义。

这是确切错误的image

这是我的 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 数据库的父母和子女的picture

由于错误表明事件未定义,我试图找出未定义的事件。

这里有什么问题?

【问题讨论】:

    标签: javascript android node.js firebase-cloud-messaging


    【解决方案1】:

    您还没有在此代码块中定义事件:

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

    【讨论】:

    • 我该如何解决这个问题??
    猜你喜欢
    • 2018-01-06
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 2017-02-18
    • 2017-07-03
    • 1970-01-01
    相关资源
    最近更新 更多