如何避免我的 Firebase Cloud Function 中的嵌套承诺 [重复]

Posted

技术标签:

【中文标题】如何避免我的 Firebase Cloud Function 中的嵌套承诺 [重复]【英文标题】:How to avoid nested promise in my Firebase Cloud Function [duplicate] 【发布时间】:2019-06-07 06:34:32 【问题描述】:

首先,当“TotalMoney”节点被触发时,该函数会从“OldNewKey”中获取一个节点,然后再获取另一个节点并进行更新。但这是警告避免嵌套承诺。

exports.postMoneyUpdater = functions.database.ref('/TotalMoney/fixedPostId/').onWrite((change, context) => 

     const fixedPostId = context.params.fixedPostId;

     const moneyAmountBefore = change.before.val();
     const moneyAmountAfter = change.after.val();
     var oldPostKey;
     // console.log("time "+ Date.now());

     if(moneyAmountAfter>moneyAmountBefore)

         const oldNewKeyRef = admin.database().ref(`/OldNewKey`).child(fixedPostId);

          return oldNewKeyRef.once('value').then((oldNewKeySnapshot)=>

              if(!oldNewKeySnapshot.exists())
                   oldPostKey = fixedPostId;
              else
                   oldPostKey = oldNewKeySnapshot.val();
               

             const postRef = admin.database().ref(`/Posts`).child(oldPostKey);

              return postRef.once('value').then((postSnapshot)=>

                  var postMap =;

                  postSnapshot.forEach((child) =>
                     postMap[child.key] = child.val();
                    );

                  const newPostKey = 9999999999999-Date.now();

                  var updateMap = ;
                  updateMap["post"] = postMap["post"];
                  updateMap["imageUrl"] = postMap["imageUrl"];
                  updateMap["userId"] = postMap["userId"];
                  updateMap["postId"] = postMap["postId"];
                  updateMap["dist"] = postMap["dist"];
                  updateMap["customId"] = postMap["customId"];
                  updateMap["newPostKey"] = newPostKey.toString();;
                  updateMap["money"] = moneyAmountAfter;

                  var writeMap = ;

                   writeMap['/Posts/'+oldPostKey] = null;
                   writeMap['/Locality/'+postMap["dist"]+'/'+oldPostKey] = null;

                   writeMap['/Posts/'+newPostKey] = updateMap;
                   writeMap['/Locality/'+postMap["dist"]+'/'+newPostKey] = updateMap;
                   writeMap['/MyPosts/'+fixedPostId] = updateMap;
                   writeMap['/OldNewKey/'+fixedPostId] = newPostKey.toString();;

                   return admin.database().ref().update(writeMap);

              );

          );

     else
         return null;
     

 );

【问题讨论】:

【参考方案1】:

以下应该没问题(但未经测试!)。

exports.postMoneyUpdater = functions.database
  .ref('/TotalMoney/fixedPostId/')
  .onWrite((change, context) => 
    const fixedPostId = context.params.fixedPostId;

    const moneyAmountBefore = change.before.val();
    const moneyAmountAfter = change.after.val();
    var oldPostKey;
    // console.log("time "+ Date.now());

    if (moneyAmountAfter > moneyAmountBefore) 
      const oldNewKeyRef = admin
        .database()
        .ref(`/OldNewKey`)
        .child(fixedPostId);

      return oldNewKeyRef
        .once('value')
        .then(oldNewKeySnapshot => 
          if (!oldNewKeySnapshot.exists()) 
            oldPostKey = fixedPostId;
           else 
            oldPostKey = oldNewKeySnapshot.val();
          

          const postRef = admin
            .database()
            .ref(`/Posts`)
            .child(oldPostKey);

          return postRef.once('value');
        )
        .then(postSnapshot => 
          var postMap = ;

          postSnapshot.forEach(child => 
            postMap[child.key] = child.val();
          );

          const newPostKey = 9999999999999 - Date.now();

          var updateMap = ;
          updateMap['post'] = postMap['post'];
          updateMap['imageUrl'] = postMap['imageUrl'];
          updateMap['userId'] = postMap['userId'];
          updateMap['postId'] = postMap['postId'];
          updateMap['dist'] = postMap['dist'];
          updateMap['customId'] = postMap['customId'];
          updateMap['newPostKey'] = newPostKey.toString();
          updateMap['money'] = moneyAmountAfter;

          var writeMap = ;

          writeMap['/Posts/' + oldPostKey] = null;
          writeMap['/Locality/' + postMap['dist'] + '/' + oldPostKey] = null;

          writeMap['/Posts/' + newPostKey] = updateMap;
          writeMap[
            '/Locality/' + postMap['dist'] + '/' + newPostKey
          ] = updateMap;
          writeMap['/MyPosts/' + fixedPostId] = updateMap;
          writeMap['/OldNewKey/' + fixedPostId] = newPostKey.toString();

          return admin
            .database()
            .ref()
            .update(writeMap);
        );
     else 
      return null;
    
  );

【讨论】:

我认为这个答案会很好。感谢你的回复。经过一段时间的反复试验,我也能够解决我的问题。下面是经过一些修改和一些额外功能的代码-drive.google.com/open?id=1Q2MJ9lvoyEAZsn0bNjYj65xoEO1HKNDh

以上是关于如何避免我的 Firebase Cloud Function 中的嵌套承诺 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何将我的 Firebase 实时数据库转移到 Firebase Cloud Firestore

如何将我的 Firebase 实时数据库转移到 Firebase Cloud Firestore

Flutter Firebase Cloud Messaging 如何自动关闭/取消通知?

如何将来自不同组件的 firebase auth 和 Cloud Firestore 用作单个 Firebase 应用程序

如何在 Cloud Functions for Firebase 中使 HTTP 请求异步/等待?

如何直接从您的 Angular 项目中调用 Firebase Cloud Function