Skip to content Skip to sidebar Skip to footer

How To Fix "function Returned Undefined, Expected Promise Or Value" When Creating A Function For Firebase?

I created a Firebase function to trigger push notifications, but I get the following register on Firebase,'Function returned undefined, expected Promise or value' and 'snapshot.doc

Solution 1:

You're not returning any value from the top-level function definition. This means that the Google Cloud Functions environment has no way to know when your function is done, so it has no way to know when to stop charging you for its use.

To make sure Cloud Functions knows when your code is done, you need to either return a value from the top-level function (in the case that all work happens synchronously), or return a promise from the top-level function (in the case that some work continues after the closing } of the function).

Right now the top-level code in your Cloud Function is not returning anything, which means that the result of calling this function is undefined. And that's what the Cloud Functions environment complains about.

In your case, you're loading data from Firestore, which is an asynchronous operation, so you will need to return a promise:

exports.destaquesTrigger = functions.firestore.document('destaques/{destaquesId}').onCreate((snapshot, context) => {
msgData = snapshot.data();

return admin.firestore().collection('pushtokens').get().then((snapshots) => 
{
   var tokens = [];
   if (snapshots.empty) {
        console.log('No Devices');
        returnfalse;
     } else {
      for (var token of snapshot.docs) {
          tokens.push(token.data().devtoken);
      }

      var payload = {
         "notification": {
             "title": msgData.notif1,
             "body": msgData.notif2,
             "sound": "default"
          },
          "data": {
             "sendername": msgData.notif3,
             "message": msgData.notif4,
         }
       }

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

    })
  })

The changes I made:

  • Added a return to return admin.firestore().collection('pushtokens').get(). This is needed so that your return admin.messaging().sendToDevice( can "bubble up" to be returned to Cloud Functions.

  • Added a return false in case there are no snapshots, as otherwise you're not returning anything.

Solution 2:

I am assuming that Mr. Kanitz has solved his issue. But since no one answered the latter part of his question, I may have a possible solution. Change the following snippet:

(var token of snapshot.docs)

to:

(var token of snapshots.docs)

I think you used the wrong variable to get docs from! Hope that helps anyone else following Raja Yogan's FCM tutorial!

Solution 3:

Your branch under if (snapshots.empty) does not return a defined value. The error message indicates it expects you to return a Promise value. Try returning Promise.resolve() in that branch.

Post a Comment for "How To Fix "function Returned Undefined, Expected Promise Or Value" When Creating A Function For Firebase?"