MarkWalczak
MarkWalczak

Reputation: 1571

Cannot read property 'val' of undefined

I try to learn how to send a notifications using a firebase functions. So I'm using this example code:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendAdminNotification = functions.database.ref('/message').onWrite(event => {
const news = event.data.val();
     const payload = {notification: {
         title: 'New news',
         body: `${news.title}`
         }
     };
return admin.messaging().sendToTopic("News",payload)
    .then(function(response){
         return console.log('Notification sent successfully:',response);
    }) 
    .catch(function(error){
         console.log('Notification sent failed:',error);
    });

});

but it gives me an error: Cannot read property 'val' of undefined I've been looking for a solution, tried this, but still no-go...

Any suggestions would be greatly appreciated.

Upvotes: 0

Views: 1689

Answers (1)

sanketd617
sanketd617

Reputation: 809

I think you are not using the syntax as specified in the documentation.

Refer this documentation.

Anyways, as I see in the documentation linked above, your code should be :

exports.sendAdminNotification = functions.database.ref('/message')
    .onWrite((change, context) => { 
        console.log(change);
        // Do something with change or context
    });

Upvotes: 3

Related Questions