1
votes

I'm trying to deploy Firebase but i'm getting this error

error Each then() should return a value or throw promise/always-return

this is my code

'use strict'

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


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




  const user_id = event.params.user_id;
  const notification_id = event.params.notification_id;

  console.log('We have a notification from : ', user_id);



  if(!event.data.val()){

    return console.log('A Notification has been deleted from the database : ', notification_id);

  }


  const fromUser = admin.database().ref(`/notifications/${user_id}/${notification_id}`).once('value');

  return fromUser.then(fromUserResult => {

    const from_user_id = fromUserResult.val().from;

    console.log('You have new notification from  : ', from_user_id);


    const userQuery = admin.database().ref(`Users/${from_user_id}/name`).once('value');
    const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

    return Promise.all([userQuery, deviceToken]).then(result => {

      const userName = result[0].val();
      const token_id = result[1].val();


      const payload = {
        notification: {
          title : "New Friend Request",
          body: `${userName} has sent you request`,
          icon: "default",
          click_action : "in.tvac.akshaye.lapitchat_TARGET_NOTIFICATION"
        },
        data : {
          from_user_id : from_user_id
        }
      };



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

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

      });

    });

  });

});

this is the error log

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 npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\PC\AppData\Roaming\npm-cache_logs\2018-11-09T12_26_46_311Z-debug.log Error: functions predeploy error: Command terminated with non-zero exit code1

1
try return null; after console.log('This was the notification Feature');James Poag
@JamesPoag Omg , you are a super hero ;) how can i close this quistion ?Waseem Ha
I posted a cleanup that I ran through ESLint. Note that I'm chaining then instead of nesting, and that I added catchJames Poag
@JamesPoag thanks a lot , problem solved :DWaseem Ha

1 Answers

2
votes

Chain then instead of nesting, include catch block, always return from then.

'use strict'

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

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

            const user_id = event.params.user_id;
            const notification_id = event.params.notification_id;

            console.log('We have a notification from : ', user_id);

            if (!event.data.val())
                return console.log(`A Notification has been deleted from the database : ${notification_id}`);

            return admin.database().ref(`/notifications/${user_id}/${notification_id}`).once('value')
                .then(fromUserResult => {

                    const from_user_id = fromUserResult.val().from;

                    console.log(`You have new notification from  : ${from_user_id}`);

                    const userQuery = admin.database().ref(`Users/${from_user_id}/name`).once('value');
                    const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

                    return Promise.all([userQuery, deviceToken]);
                })
                .then(result => {

                    const userName = result[0].val();
                    const token_id = result[1].val();

                    const payload = {
                        notification: {
                            title: "New Friend Request",
                            body: `${userName} has sent you request`,
                            icon: "default",
                            click_action: "in.tvac.akshaye.lapitchat_TARGET_NOTIFICATION"
                        },
                        data: {
                            from_user_id: from_user_id
                        }
                    };

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