简体   繁体   English

每个 then() 应该在 firebase 云 function 中返回一个值或抛出错误

[英]Each then() should return a value or throw error in firebase cloud function

I am writing a cloud function for firebase using javascript, while I deploy a function a error occurring and terminate deploy process.我正在使用 javascript 为 firebase 编写云 function,而我部署了一个 ZC1C425268E68385D1AB5074C4 发生错误并终止 aC4 进程。

Error: Each then() should return a value or throw错误:每个 then() 都应该返回一个值或抛出

How I resolve this error?我如何解决这个错误?

Below I attached my code.下面我附上了我的代码。

exports.sendNotificationForLikeOrFollow = functions.database.ref('/USER_MANAGEMENT/USER_ACTIVITY/{activityId}').onCreate((snap, context) => {

  var type = snap.val().ACTIVITY_TYPE;

  if (type=='Comment') {
    return; //There is a separate function for comment notification
  }

  if (type=='Like') {

    const likeUserId = snap.val().BY_USER_NODE_NAME;
    const publishedUserId = snap.val().PUBLISHED_USER_NODE_NAME;
    const puplishedContentId = snap.val().LIKE_PUBLISHED_CONTENT_NODE_NAME;
    const likedUserName = snap.val().BY_USER_NAME;
    const likedUserPhotoUrl = snap.val().BY_USER_PHOTO_URL;

    // var publishedUserRef = event.data.ref.parent.parent.child('USERS/'+publishedUserId);
    // var likedUserRef = event.data.ref.parent.parent.child('USERS/'+likeUserId);
    var publishedUserRef = db.ref("USER_MANAGEMENT/USERS/"+publishedUserId);
    var likedUserRef = db.ref("USER_MANAGEMENT/USERS/"+likeUserId);

    return Promise.all([publishedUserRef.once('value')]).then(function(snaps) {

        var data = snaps[0].val();
        const fcmToken = data.FCM_TOKEN;

          // Notification details.
          const payload = {
            notification: {
              title: '❤️ You got a new like!',
              body: likedUserName+` liked your artwork.`,
              sound: 'default',
              icon: '',
              byUserId: likeUserId,
              byUserName: likedUserName,
              type: 'Like',
              likedPuplishedContentId: puplishedContentId,
              publishedUserId: publishedUserId,
              byUserPhotoUrl: likedUserPhotoUrl,
              badge : '1'
            }
          };

          // Listing all tokens.
          const tokens = fcmToken;

          // Send notifications to all tokens.
          return admin.messaging().sendToDevice(tokens, payload).then(response => {
            // For each message check if there was an error.
            response.results.forEach((result, index) => {
              console.log('FcmToken: ', tokens);
              const error = result.error;
              if (error) {
                console.log('Error Occured:', error);
              }
            });
            console.log('User Liked');
          });
    });
  }

  if (type=='Follow') {
    const followerUserId = snap.val().BY_USER_NODE_NAME;
    const followeeUserId = snap.val().FOLLOWING_USER_NODE_NAME;
    const followerUserName = snap.val().BY_USER_NAME;
    const followerPhotoUrl = snap.val().BY_USER_PHOTO_URL;

    // var followerUserRef = event.data.ref.parent.parent.child('USERS/'+followerUserId);
    // var followeeUserRef = event.data.ref.parent.parent.child('USERS/'+followeeUserId);
    var followerUserRef = db.ref('USER_MANAGEMENT/USERS/'+followerUserId);
    var followeeUserRef = db.ref('USER_MANAGEMENT/USERS/'+followeeUserId);
    var isFollow;

    //const followeeFollowingRef = event.data.ref.parent.parent.child('FOLLOWING/'+followeeUserId+'/'+followerUserId);
    const followeeFollowingRef = db.ref('USER_MANAGEMENT/FOLLOWING/'+followeeUserId+'/'+followerUserId);

    return Promise.all([followeeUserRef.once('value')]).then(function(snaps) {

        var data = snaps[0].val(); // Get whole USER_MANAGEMENT snapshot

        const fcmToken = data.FCM_TOKEN

        followeeFollowingRef.once('value').then(function(followeeSnapshot) {

          if (followeeSnapshot.exists()) {
            isFollow = 'YES';
            console.log('FOLLOW YES');
          }else{
            isFollow = 'NO';
            console.log('FOLLOW NO');
          }

            // Notification details.
            const payload = {
              notification: {
                title: '🤗 You have a new follower!',
                body: followerUserName+` is now following you.`,
                sound: 'default',
                icon: '',
                byUserId: followerUserId,
                byUserName: followerUserName,
                type: 'Follow',
                isFollow: isFollow,
                byUserPhotoUrl: followerPhotoUrl,
                badge : '1'
              }
            };

            // Listing all tokens.
            const tokens = fcmToken;
            console.log('FcmToken: ', tokens);
            // Send notifications to all tokens.
            return admin.messaging().sendToDevice(tokens, payload).then(response => {
              // For each message check if there was an error.
              response.results.forEach((result, index) => {
                const error = result.error;
                if (error) {

                  console.log('Error Occured:', error);
                }
              });
              console.log('User Followed');
            });
      });    
    });
  }
});

This function already deploy before few years and it is working fine.这个 function 已经在几年前部署并且运行良好。 But now I try to deploy same code to another project it was stopped due to above error.但是现在我尝试将相同的代码部署到另一个项目,它由于上述错误而停止。

You don't correctly chain the different promises returned by the Firebase asynchronous methods.您没有正确链接Firebase 异步方法返回的不同承诺。

Also, note that you do not need to use Promise.all() since the once() method returns only one Promise.另外,请注意,您不需要使用Promise.all() ,因为once()方法只返回一个 Promise。 Again, you should correctly chain the Promises, instead of using Promise.all() , which should be use for executing asynchronous methods in parallel , and not in sequence.同样,您应该正确链接Promise,而不是使用Promise.all() ,它应该用于并行执行异步方法,而不是按顺序执行。

So, the following should do the trick (untested):因此,以下应该可以解决问题(未经测试):

exports.sendNotificationForLikeOrFollow = functions.database.ref('/USER_MANAGEMENT/USER_ACTIVITY/{activityId}').onCreate((snap, context) => {

    var type = snap.val().ACTIVITY_TYPE;

    if (type == 'Comment') {
        return null; //There is a separate function for comment notification
    }

    if (type == 'Like') {

        const likeUserId = snap.val().BY_USER_NODE_NAME;
        const publishedUserId = snap.val().PUBLISHED_USER_NODE_NAME;
        const puplishedContentId = snap.val().LIKE_PUBLISHED_CONTENT_NODE_NAME;
        const likedUserName = snap.val().BY_USER_NAME;
        const likedUserPhotoUrl = snap.val().BY_USER_PHOTO_URL;

        var publishedUserRef = db.ref("USER_MANAGEMENT/USERS/" + publishedUserId);

        return publishedUserRef.once('value')
            .then(snaps => {

                var data = snaps[0].val();
                const fcmToken = data.FCM_TOKEN;

                // Notification details.
                const payload = {
                    notification: {
                        title: '❤️ You got a new like!',
                        body: likedUserName + ` liked your artwork.`,
                        sound: 'default',
                        icon: '',
                        byUserId: likeUserId,
                        byUserName: likedUserName,
                        type: 'Like',
                        likedPuplishedContentId: puplishedContentId,
                        publishedUserId: publishedUserId,
                        byUserPhotoUrl: likedUserPhotoUrl,
                        badge: '1'
                    }
                };

                // Listing all tokens.
                const tokens = fcmToken;

                // Send notifications to all tokens.
                return admin.messaging().sendToDevice(tokens, payload);
            })
            .then(response => {
                // For each message check if there was an error.
                response.results.forEach((result, index) => {
                    console.log('FcmToken: ', tokens);
                    const error = result.error;
                    if (error) {
                        console.log('Error Occured:', error);
                    }
                });
                console.log('User Liked');
                return null;
            });
    }

    if (type == 'Follow') {
        // See above, it is similar
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 部署Firebase云函数时出错每个then()应该返回一个值或抛出 - Error on deploy Firebase cloud function Each then() should return a value or throw 在部署 Firebase 云 Function 时收到“每个都应该返回值或抛出”错误 - On Deploying the Firebase Cloud Function getting an error of “Each then should return a value or throw” 得到错误每个 then() 应该返回一个值或抛出我的 firebase function - getting error Each then() should return a value or throw in my firebase function 每个 then() 都应该返回一个值或抛出 Firebase 云函数 - Each then() should return a value or throw Firebase cloud functions 部署 Firebase 函数时出错:每个 then() 都应该返回一个值或抛出 promise/always-return - Error deploying Firebase function: Each then() should return a value or throw promise/always-return Firebase云消息传递每个then()应该返回一个值或抛出Promise / Always-Return - Firebase cloud messaging Each then() should return a value or throw promise/always-return 错误每个 then() 应该返回一个值或抛出 - Error Each then() should return a value or throw 在使用node.js中的firebase从Google云端存储中读取时,ESlint发出“每次应返回一个值或引发错误”的问题 - ESlint issue “Each then should return a value or throw” while reading from google cloud store using firebase in node.js 每个 then() 应该返回一个值或抛出 - Each then() should return a value or throw Node.js 错误:每个 then() 应该返回一个值还是抛出? - Node.js error: Each then() should return a value or throw?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM