如何避免Firebase Cloud Function中的嵌套承诺

时间:2019-01-12 16:46:38

标签: android firebase firebase-realtime-database firebase-cloud-messaging google-cloud-functions

首先,当“ 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 个答案:

答案 0 :(得分: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;
    }
  });