Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 then() should return a value or throw promise/always-return

'use strict'

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((change, context) => {

    const user_id = context.params.user_id;
    const notification_id = context.params.notification_id;
    console.log('We have a notification from : ', user_id);

    if (!change.after.val()) {
        return console.log('A Notification has been deleted from the database : ', notification_id);
    }
    const deviceToken = admin.database().ref(`/ServiceProvider/${user_id}/device_token`).once('value');
    return deviceToken.then(result => {
        const token_id = result.val();
        const payload = {
            notification: {
              title : "New Friend Request",
              body: "You Have Received A new Friend Request",
              icon: "default"
            }
        };

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

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

        });

    });

});
like image 434
Ikram Khan Niazi Avatar asked Nov 17 '18 18:11

Ikram Khan Niazi


2 Answers

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');
        return null;   // 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.

like image 62
Doug Stevenson Avatar answered Sep 22 '22 05:09

Doug Stevenson


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');
      return true;
    },err=>
    {
      throw err;
    });

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

like image 43
Peter Haddad Avatar answered Sep 23 '22 05:09

Peter Haddad