简体   繁体   English

Firebase Cloud Messaging:未定义事件

[英]Firebase Cloud Messaging: event is not defined

I've got a problem with Firebase Cloud Messaging notifications. 我有Firebase Cloud Messaging通知问题。 When I want to send friend request the other client doesn't receive a notification. 当我想发送好友请求时,其他客户端没有收到通知。 And Firebase Functions log says: Firebase功能日志显示:

ReferenceError: event is not defined
    at exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:14:8)
    at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:109:23)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:139:20)
    at /var/tmp/worker/worker.js:728:24
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

Here is JavaScript code: 这是JavaScript代码:

'use strict'

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

exports.sendNotification = functions.database.ref('/Notifications/{receiver_id}/{notification_id}').onWrite((change,context) => {
 const receiver_id = context.params.receiver_id;

 const notification_id = context.params.notification_id;

console.log('We have a notification to send to: ', receiver_id);

  if (!event.data.val) {
    return console.log('A notification has been deleted from database: ', notification_id);
  }

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

  return deviceToken.then(result => {
    const token_id = result.val();

    const payload = {
      notification:
      {
        title: "Friend Request",
        body: "you have received a new friend request",
        icon: "default"
      }
    };

    return admin.messaging().sendToDevice(token_id, payload).then(response => {
      console.log('This was the notification feature.');
    });
  });

});

Change this: 更改此:

if (!event.data.val) {
return console.log('A notification has been deleted from database: ', notification_id);
 }

into this: 到这个:

if (!change.after.val()) {
return console.log('A notification has been deleted from database: ', notification_id);
 }

The change object has two properties after and before , each of these is a DataSnapshot with the same methods available in admin.database.DataSnapshot . change对象after beforebefore具有两个属性,每个属性都是一个DataSnapshot具有与admin.database.DataSnapshot可用的相同方法。

Also val() is a method not a property. val()也是一种方法,而不是属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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