Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with Firebase deploy function to send push notifications

I'm developing an iOS app and now I'm stuck with Firebase deploy functions. I'm trying to send push notifications and I prepared the codes like below.

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

exports.pushNotifications = functions.database.ref('/messages/{messageId}')
    .onCreate(event => {

        const data = event.data;
        const fromId = data.fromId;
        const toId = data.toId;
        const message = data.message;

        console.log(fromId + ' sent a message to' + toId);

        return admin.database().ref('/users/' + fromId).once('value', snapshot => {

            var user = snapshot.val();

            var payload = {
                notification: {
                    title: user.username,
                    body: message
                }
            }

            admin.messaging().sendToDevice(user.fcmToken, payload)
                .then(function(response) {
                    // See the MessagingDevicesResponse reference documentation for
                    // the contents of response.
                    console.log("Successfully sent message:", response);
                })
                .catch(function(error) {
                    console.log("Error sending message:", error);
                });

        })

Database structure:

messages - messageId -fromId
                     └toId
                     └Message

         └ messageId -fromId
                     └toId
                     └Message
             .
             .
             .

And this is the error message.

37:1  error  Parsing error: Unexpected token

✖ 1 problem (1 error, 0 warnings)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/...

Error: functions predeploy error: Command terminated with non-zero exit code1

Also in the log, I get errors like:

TypeError: Cannot read property 'fromId' of undefined

Is the error occurring because I'm not fetching fcmToken right?

I've never coded with JavaScirpt. I would appreciate any suggestion!

like image 471
UK4 Avatar asked Apr 12 '18 08:04

UK4


1 Answers

Change this:

exports.pushNotifications = functions.database.ref('/messages/{messageId}')
.onCreate(event => {

    const data = event.data;
    const fromId = data.fromId;
    const toId = data.toId;
    const message = data.message;

into this:

exports.pushNotifications = functions.database.ref('/messages/{messageId}')
.onCreate((snap,context) => {

    const data = snap.val();
    const fromId = data.fromId;
    const toId = data.toId;
    const message = data.message;
});

Check here for more info:

https://firebase.google.com/docs/functions/beta-v1-diff

like image 153
Peter Haddad Avatar answered Nov 03 '22 01:11

Peter Haddad