0

I'm a beginner at nodeJs but trying to connect to cloud server to send notification trough firebase cloud notifications

   // The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.helloWorld = functions.https.onRequest((request, response) => {
 response.send("Hello from the ProjectSupporter Team");
});

exports.sendPushNotifications = functions.https.onRequest((req, res) => {
    res.send("Attempting to send push notification...")
    console.log("LOGGER --- Trying to send push message...")

    // admin.message().sendToDevice()

    var uid = 'Ky6h0BgpGDNMUyyBgE4ul99MzXk1'

    return admin.database().ref('/users/' + uid).once('value', async snapshot => {
        var user = snapshot.val();
        console.log("User username: " + user.username + " fcmToken: " + user.fcmToken);

        // See documentation on defining a message payload.
        var message = {
            notification: {
                title: "Push Notification Title",
                body: "Message Body..."
            },
          data: {
            score: '850',
            time: '2:45'
          },
          token: user.fcmToken
        };

        // Send a message to the device corresponding to the provided
        // registration token.
       try {
       const response = await admin.messaging().send(message);
       console.log('Successfully sent message:', response);
   } catch(e) {
       console.log('Error sending message:', error);
   }
});

This is the error message I'm getting:

20:70 error Parsing error: Unexpected token snapshot

✖ 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.

My ide Brackets says there is an error on line 20, 70. I can't figure out what the problem is.

Here is an image: https://ibb.co/iBp17H

Thanks in advance!

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
Simon74
  • 3
  • 2

1 Answers1

0

The error you are getting it's an eslint error, if you run your code it will work, since it's valid javascript.

The linter rule promise/always-return is asking you to always return inside a promise chain, even in the last .then. So just return something or use async/await.

return admin.database().ref('/users/' + uid).once('value', snapshot => {
    /*...*/
    // Send a message to the device corresponding to the provided
    // registration token.
    return admin.messaging().send(message)
        .then((response) => {
             //  promise/always-return
             console.log('Successfully sent message:', response);
             // Do something with response
             return true; // To quiet linter.
        })
        .catch((error) => {
            console.log('Error sending message:', error);
        });
});

Using async/await (You'll need to transpile your code first)

Cloud Functions for Firebase Async Await style

return admin.database().ref('/users/' + uid).once('value', async snapshot => {
    /*...*/
    // Send a message to the device corresponding to the provided
    // registration token.

    try {
        const response = await admin.messaging().send(message);
        console.log('Successfully sent message:', response);
    } catch(e) {
        console.log('Error sending message:', error);
    }
});
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98