Skip to content Skip to sidebar Skip to footer

Each Then() Should Return A Value Or Throw Firebase Cloud Functions

I am writing a cloud function for firebase using javascript but I am stuck, I don't know the exact meaning of error and unable to solve it.. The error states: 27:65 error Each

Solution 1:

Change this:

return admin.messaging().sendToDevice(token_id, payload).then(response => {

        console.log('This was the notification Feature');

    });

To this:

return admin.messaging().sendToDevice(token_id, payload).then(response => {

        console.log('This was the notification Feature');
        returnnull;   // add this line

    });

The then callback just needs to return a value.

However, eslint may then complain about nested then() in your code, which is also an anti-pattern. Your code should really be structured more like this:

const deviceToken = admin.database().ref(`/ServiceProvider/${user_id}/device_token`).once('value');
return deviceToken.then(result => {
    // redacted stuff...return admin.messaging().sendToDevice(token_id, payload);
}).then(() => {
    console.log('This was the notification Feature');
});

Note that each then chains off of each other, rather than being nested inside each other.

Solution 2:

Change this:

return admin.messaging().sendToDevice(token_id, payload).then(response => {

    console.log('This was the notification Feature');

  });

into this:

return admin.messaging().sendToDevice(token_id, payload).then(response=>{
      console.log('This was the notification Feature');
      returntrue;
    },err=>
    {
      throw err;
    });

As the error says when using then you need to return a value.

Solution 3:

That's jslinting telling you that each .then must include a return value. In other words, avoid promise anti pattern.

You might find async functions easier to wrap your head around. Note that you'll need to run the Node 8 runtime for async support though...

Post a Comment for "Each Then() Should Return A Value Or Throw Firebase Cloud Functions"