最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase Function:Function returned undefined, expected Promise or value - Stack Overflow

programmeradmin2浏览0评论

I have written a firebase function to send notification whenever a like occurs in my android app. The notification functionality works good most of the times but sometimes does not work.

I receive this error always ( whether it is working or not):

Function returned undefined, expected Promise or value

Here's the code of my like function:

exports.sendactorLikeNotification = functions.database.ref('/Likes/{post_id}/{user_id}')
.onWrite(event => {


   if (event.data.exists()) 
    {

      const message = event.data.val();
      const userUid = event.params.user_id;

      const ownerUid = message.owner_id;
      console.log("Owner id", ownerUid);
      const userPic = message.thumb_image;
      const userName = message.name;
      const post_key = event.params.post_id;
      const timestamp = admin.database.ServerValue.TIMESTAMP;

      if(ownerUid == userUid){return null;}

      const Promise1= admin.database().ref(`/notifs/${ownerUid}`).push({
        thumb_image: userPic,
        name: userName,
        user_id: userUid,
        post_id: post_key,
        text: "liked your post",
        type: "Like",
        read: "false",
        time: timestamp
     });

      const Promise2=admin.database().ref(`/Users/${ownerUid}/device_token`).once('value');

      const Promise3= Promise2.then(function(snapshot) {
          const getrealDeviceTokensPromise = snapshot.val();
          console.log("Device Token", getrealDeviceTokensPromise);

          // Notification details.
          const payload = {
                            notification: {
                                              title: 'Appname',
                                              body:  userName + ' has liked your post.',
                                              icon: "default",
                                              sound: "default",
                                              click_action: "OPEN_ACTIVITY_1"
                                          }
                        };

          const Promise4= admin.messaging().sendToDevice(getrealDeviceTokensPromise, payload)
                   .then(function (response) {
                       console.log("Successfully sent message:", response);
                       return Promise.all([Promise1,Promise3,Promise4]);
                   })
                   .catch(function (error) {
                       console.log("Error sending message:", error);
                       return null;
                   });

      }, function(error) {
      // The Promise was rejected.
          console.error(error);
          return null;
      });

    }

    else
    {
      return null;
    } 


});

I don't understand where I am going wrong. Please help!

I have written a firebase function to send notification whenever a like occurs in my android app. The notification functionality works good most of the times but sometimes does not work.

I receive this error always ( whether it is working or not):

Function returned undefined, expected Promise or value

Here's the code of my like function:

exports.sendactorLikeNotification = functions.database.ref('/Likes/{post_id}/{user_id}')
.onWrite(event => {


   if (event.data.exists()) 
    {

      const message = event.data.val();
      const userUid = event.params.user_id;

      const ownerUid = message.owner_id;
      console.log("Owner id", ownerUid);
      const userPic = message.thumb_image;
      const userName = message.name;
      const post_key = event.params.post_id;
      const timestamp = admin.database.ServerValue.TIMESTAMP;

      if(ownerUid == userUid){return null;}

      const Promise1= admin.database().ref(`/notifs/${ownerUid}`).push({
        thumb_image: userPic,
        name: userName,
        user_id: userUid,
        post_id: post_key,
        text: "liked your post",
        type: "Like",
        read: "false",
        time: timestamp
     });

      const Promise2=admin.database().ref(`/Users/${ownerUid}/device_token`).once('value');

      const Promise3= Promise2.then(function(snapshot) {
          const getrealDeviceTokensPromise = snapshot.val();
          console.log("Device Token", getrealDeviceTokensPromise);

          // Notification details.
          const payload = {
                            notification: {
                                              title: 'Appname',
                                              body:  userName + ' has liked your post.',
                                              icon: "default",
                                              sound: "default",
                                              click_action: "OPEN_ACTIVITY_1"
                                          }
                        };

          const Promise4= admin.messaging().sendToDevice(getrealDeviceTokensPromise, payload)
                   .then(function (response) {
                       console.log("Successfully sent message:", response);
                       return Promise.all([Promise1,Promise3,Promise4]);
                   })
                   .catch(function (error) {
                       console.log("Error sending message:", error);
                       return null;
                   });

      }, function(error) {
      // The Promise was rejected.
          console.error(error);
          return null;
      });

    }

    else
    {
      return null;
    } 


});

I don't understand where I am going wrong. Please help!

Share Improve this question edited Jun 28, 2018 at 11:23 Maverick7 asked Jun 26, 2018 at 21:34 Maverick7Maverick7 1,09712 silver badges23 bronze badges 4
  • A couple observations. 1) Your code is difficult to follow the way it's formatted here. 2) You're using a really old version of the firebase-functions SDK - consider upgrading to the latest 1.x version and migrate your code to the new APIs. – Doug Stevenson Commented Jun 26, 2018 at 21:45
  • Hey, sorry for the shabby formatting. I have improved it in my edit. – Maverick7 Commented Jun 26, 2018 at 21:58
  • I don't think you have all the code in there. Last time you had a call to Promise.all. – Doug Stevenson Commented Jun 26, 2018 at 22:01
  • Yes, I missed it. Here's the the entire code now. Kindly look into it – Maverick7 Commented Jun 26, 2018 at 22:03
Add a ment  | 

3 Answers 3

Reset to default 2
exports.sendactorLikeNotification = functions.database.ref('/Likes/{post_id}/{user_id}').onWrite(event => {
  if (event.data.exists()) {

  const promises=[];



  const message = event.data.val();
  const userUid = event.params.user_id;

  const ownerUid = message.owner_id;
  console.log("Owner id", ownerUid);
  const userPic = message.thumb_image;
  const userName = message.name;
  const post_key = event.params.post_id;
  const timestamp = admin.database.ServerValue.TIMESTAMP;

  if(ownerUid == userUid){return null;}

    const a1=admin.database().ref(`/notifs/${ownerUid}`).push({
    thumb_image: userPic,
    name: userName,
    user_id: userUid,
    post_id: post_key,
    text: "liked your post",
    type: "Like",
    read: "false",
    time: timestamp
 });

  promises.push(a1);




   const a2= admin.database().ref(`/Users/${ownerUid}/device_token`).once('value').then(function(snapshot) {






      const getrealDeviceTokensPromise = snapshot.val();

      console.log("Device Token", getrealDeviceTokensPromise);

      // Notification details.
      const payload = {
        notification: {
          title: 'Appname',
          body:  userName + ' has liked your post.',
          icon: "default",
          sound: "default",
          click_action: "OPEN_ACTIVITY_1"
        }
      };

      const a3=admin.messaging().sendToDevice(getrealDeviceTokensPromise, payload)
               .then(function (response) {
                   console.log("Successfully sent message:", response);
               })
               .catch(function (error) {
                   console.log("Error sending message:", error);
               });


               promises.push(a3);


  }, function(error) {

      console.error(error);
  });
   promises.push(a1);
  return Promise.all(promises);

}

else
{
  return null;
}

});

This code solved the problem for me!

You're returning undefined when:

  1. event.data.exists() returns false
  2. ownerUid == userUid

You're also not dealing with the promise returned by sendToDevice().then().catch(). The function needs to wait until that work is done before terminating.

Please test the following changes and let me know, also I remend updating the firebase-functions SDK:

exports.sendactorLikeNotification = functions.database.ref('/Likes/{post_id}/{user_id}')
  .onWrite(event => {
    if (event.data.exists()) {
      const promises = [];
      const message = event.data.val();
      const userUid = event.params.user_id;
      const ownerUid = message.owner_id;
      const userPic = message.thumb_image;
      const userName = message.name;
      const post_key = event.params.post_id;
      const timestamp = admin.database.ServerValue.TIMESTAMP;
      if (ownerUid === userUid) return null;
      return Promise.all([admin.database().ref(`/Users/${ownerUid}/device_token`).once('value')]).then(r => {
        const cO = r[0];
        const aP = admin.database().ref(`/notifs/${ownerUid}`).push({
          thumb_image: userPic,
          name: userName,
          user_id: userUid,
          post_id: post_key,
          text: "liked your post",
          type: "Like",
          read: "false",
          time: timestamp
        });
        promises.push(aP);
        const payload = {
          notification: {
            title: 'Appname',
            body: userName + ' has liked your post.',
            icon: "default",
            sound: "default",
            click_action: "OPEN_ACTIVITY_1"
          }
        };
        const tokensList = Object.keys(cO.val());
        promises.push(admin.messaging().sendToDevice(tokensList, payload));
        return Promise.all(promises);
      });
    }
    return null;
  });
发布评论

评论列表(0)

  1. 暂无评论