具有Firebase云功能的无服务器通知

时间:2018-07-31 17:03:28

标签: node.js firebase google-cloud-functions

我使用了Firebase聊天通知云功能,但是当  通知已触发,我在Firebase函数中遇到此错误  控制台

  

无法读取未定义的属性“当前”

     

在exports.sendNotification.functions.database.ref.onWrite.event(/user_code/index.js:8:35)

这是我的职能:

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

exports.sendNotification = functions.database.ref('/notifications/messages/{pushId}').onWrite(event => {
    console.log('Push notification event triggered for testing');
    console.log(event);
    const message = event.data.current.val();
    const senderUid = message.from;
    const receiverUid = message.to;
    console.log(receiverUid);
    const promises = [];

    if (senderUid === receiverUid) {
        //if sender is receiver, don't send notification
        promises.push(event.data.current.ref.remove());
        return Promise.all(promises);
    }

    const getInstanceIdPromise = admin.database().ref(`/usersnew/${receiverUid}`).once('value');
    const getSenderUidPromise = admin.auth().getUser(senderUid);

    return Promise.all([getInstanceIdPromise, getSenderUidPromise]).then(results => {
        const instanceId = results[0].val();
        const sender = results[1];
        console.log('notifying ' + receiverUid + ' about ' + message.body + ' from ' + senderUid);

        const payload = {
            notification: {
                title: sender.displayName,
                body: message.body,
                icon: sender.photoURL
            }
        };

         admin.messaging().sendToDevice(instanceId, payload)
            .then(function (response) {
                return console.log("Successfully sent message:", response);
            })
            .catch(function (error) {
                console.log("Error sending message:", error);
            });

            return console.log('This is the notify feature');
    });
});

有人知道如何解决吗? 当我记录事件时,它在控制台中如下所示

{ before: 
   DataSnapshot {
 app: 
  FirebaseApp {
    firebaseInternals_: [Object],
    services_: {},
    isDeleted_: false,
    name_: '__admin__',
    options_: [Object],
    INTERNAL: [Object] },
 instance: 'https://teleport-24f52.firebaseio.com',
 _path: '/notifications/messages/-LIlFNd2spo_V1rM-G-f',
 _data: null },
  after: 
   DataSnapshot {
 app: 
  FirebaseApp {
    firebaseInternals_: [Object],
    services_: {},
    isDeleted_: false,
    name_: '__admin__',
    options_: [Object],
    INTERNAL: [Object] },
 instance: 'https://teleport-24f52.firebaseio.com',
 _path: '/notifications/messages/-LIlFNd2spo_V1rM-G-f',
 _data: 
  { body: 'abc',
    dayTimestamp: 1532975400000,
    from: 'q8gtwtwXqbV2DtpsrbYajFsWzSr2',
    negatedTimestamp: -1533056068309,
    timestamp: 1533056068309,
    to: 'Cmpu7mbIENTYyoHZjCjZnbnBMbl2' } } }
10:22:44.625 PM

2 个答案:

答案 0 :(得分:3)

在您的代码中,event.data.current应该是event.after.val()event.after.ref等...

云功能1.0中的API有所更改。

阅读: https://firebase.google.com/docs/functions/database-events#reading_the_previous_value https://firebase.google.com/docs/functions/beta-v1-diff

答案 1 :(得分:2)

问题可能出在SDK版本中。从v1.0开始,情况有所变化。尝试更新SDK并按照以下说明进行迁移: https://firebase.google.com/docs/functions/beta-v1-diff