ashaneen
ashaneen

Reputation: 137

then() returns a value, still there is a return error

I have encountered a slight problem, where the errorLog is saying

21:10  warning  Avoid nesting promises                      promise/no-nesting
36:12  warning  Avoid nesting promises                      promise/no-nesting
36:66  error    Each then() should return a value or throw  promise/always-return

The error it is talking about is placed down on the bottom

 return admin.messaging().sendToDevice(token_id,payload).then(theResult =>{
                    console.log("then is returned");
                });

Even though the message is quite simple and there shouldn't be much problem with debugging (even as for newbie as I am), i can't really work it out, I tried even with deleting this function and still there is no success in deploy. I looked for some similiar questions but nothing but a failure. I will very appreciate any help, thank You.

exports.sendNotification = functions.firestore.document("Users/{user_id}/Notifications/{notification_id}").onWrite(event=>
{
    const user_id = event.params.user_id;
    const notification_id = event.params.notification_id;
    //console.log("User ID:" + user_id + "Notification ID : "+ notification_id);

    return admin.firestore().collection("Users").doc(user_id).collection("Notifications").doc(notification_id).get().then(queryResult => 
    {

        const from_user_id = queryResult.data().from;
        const from_message = queryResult.data().message;

        const from_data = admin.firestore().collection("Users").doc(from_user_id).get();
        const to_data = admin.firestore().collection("Users").doc(user_id).get();

        return Promise.all([from_data,to_data]).then(result=>
        {

            const from_name = result[0].data().name;
            const to_name = result[1].data().name;
            const token_id = result[1].data().token_id;

            const payload = {
              notification: {
                    title : "Nowa wiadomość od:" + from_name,
                    body : from_message,
                    icon : "default"
                }
            };

                return admin.messaging().sendToDevice(token_id,payload).then(theResult =>{
                    console.log("then is returned");
                });
        });


    });

});

Upvotes: 0

Views: 350

Answers (1)

palaѕн
palaѕн

Reputation: 73906

You can update the .then() block like:

.then(theResult => console.log("then is returned"));
  • always-return: A return inside each then() is needed so as to create readable and reusable Promise chains.

Upvotes: 1

Related Questions