0

I have used firebase chat notification cloud function but when notification triggered I am getting this error in firebase function console

Cannot read property 'current' of undefined

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

Here is my function:

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');
    });
});

Does anyone know how to solve this? When I log event it shows like below in console

{ 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
Community
  • 1
  • 1
Shweta Chauhan
  • 6,739
  • 6
  • 37
  • 57

2 Answers2

3

In your code, event.data.current should be event.after.val() , event.after.ref, etc...

There was a change of API in cloud functions 1.0.

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

William Chong
  • 2,107
  • 13
  • 24
2

Maybe the problem is in the SDK version. From v1.0 things change a bit. Try updating SDK and follow these instructions for miragation: https://firebase.google.com/docs/functions/beta-v1-diff

Siim Puniste
  • 210
  • 2
  • 11