简体   繁体   中英

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!

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

You are most likely running v1 of firebase functions, which is the latest version which brought quite a few changes to the api. You can read more about the changes here . Specifically, you want to change your event => parameter to (snap, context) =>

exports.dbWrite = functions.database.ref('/path').onCreate((snap, context) => {
  const data = snap.val();
  const { fromId, toId } = data;
  ...
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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