1

I want to implement push notifications automatically and I've used javascript (node.js) but I got this error

Function returned undefined, expected Promise or value

I am not node js developer I am a flutter developer and I don't know what is promises.

this is my code:

const functions = require('firebase-functions');

const admin = require('firebase-admin');


admin.initializeApp(functions.config().firebase);

var notificationMessageData;

exports.fcmTester = functions.firestore.document('posts/{postID}').onCreate((snapshot , context) => {
    notificationMessageData = snapshot.data();

    admin.firestore().collection('pushTokens').get().then(async (snapshot) => {
        var tokens = [];

        if (snapshot.empty) {
            console.log('No Devices');
        } else {
            for (var token of snapshot.docs) {
                tokens.push(token.data().tokenID);
            }

            var payload = {
                "notification": {
                    "title": "from" + notificationMessageData.writer,
                    "body": "from" + notificationMessageData.name,
                    "sound": "default"
                },
                "data": {
                    "sendername": notificationMessageData.writer,
                    "message": notificationMessageData.name
                }
            }

            return await admin.messaging().sendToDevice(tokens , payload).then((response) => {
                console.log('nice');
            }).catch((err) => {
                console.log(err);
            })
        }
    })
})




Everything is going okay and I upload it without any problem but when adding a document to the posts collection it outputs in the logs the above error.

I have created a user registration form and I've registered the users and put their token id in a collection called pushTokens and then sending a notification for each user inside that collection but this didn't work.

enter image description here

2 Answers 2

3

There are two problems in your code:

  1. You don't return the promises returned by the asynchronous Firebase methods (get() and sendToDevice());
  2. You mix-up the use of async/await with the then() method.

I would suggest you watch the 3 official videos about "JavaScript Promises" from the Firebase video series, and then that you first try with using the then() method to correctly chain your Promises and return the chain.

The following code should work.

exports.fcmTester = functions.firestore.document('posts/{postID}').onCreate((snapshot, context) => {
    const notificationMessageData = snapshot.data();

    return admin.firestore().collection('pushTokens').get()
        .then(snapshot => {
            var tokens = [];

            if (snapshot.empty) {
                console.log('No Devices');
                throw new Error('No Devices');
            } else {
                for (var token of snapshot.docs) {
                    tokens.push(token.data().tokenID);
                }

                var payload = {
                    "notification": {
                        "title": "from" + notificationMessageData.writer,
                        "body": "from" + notificationMessageData.name,
                        "sound": "default"
                    },
                    "data": {
                        "sendername": notificationMessageData.writer,
                        "message": notificationMessageData.name
                    }
                }

                return admin.messaging().sendToDevice(tokens, payload)
            }

        })
        .catch((err) => {
            console.log(err);
            return null;
        })

});

Then, after having experienced the "management" of asynchronous methods with then() (and catch()), you may give a try with async/await: again, this official video from Doug Stevenson will be of great help.

Sign up to request clarification or add additional context in comments.

1 Comment

@thecoder Hi, did you have the opportunity to look at thIs answer? Thx.
0

It says:

Function returned undefined, expected Promise or value

Have you tried returning null, or true or false, etc. from your functions?

A promise is something you pass around your program while you wait for it to become a real value.

It seems like the error message is focused on wanting you to return something from your function. Without knowing more, this is the best advice I can give.

1 Comment

I've done as you've mentioned but nothing still works and it gives me the same error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.