【问题标题】:Cannot modify a WriteBatch that has been committed in cloud functions无法修改已在云函数中提交的 WriteBatch
【发布时间】:2018-08-24 19:05:51
【问题描述】:

我在云函数上部署此代码,得到无法修改已提交的 WriteBatch,我在获取每个集合后尝试提交,但这不是正确的方法并且不一致,尝试几个小时后无法发现错误。代码在冷启动后第一次运行,这个帖子有同样的问题Batch write to firebase cloud firestore,在哪里创建

每组写入一个新批次。 在这段代码中。

 var batch = db.batch();
      db.collection("myposts")
        .doc(post_id)
        .collection("fun")
        .get()
        .then(snapshot => {
          return snapshot.forEach(doc => {
            batch.delete(doc.ref);
          });
        })
        .then(
          db
            .collection("relations_new")
            .where("uid", "==", uid)
            .get()
            .then(snapshot => {
              return snapshot.forEach(doc => {
                batch.delete(doc.ref);
              });
            })
        )
        .then(
          db
            .collection("relation")
            .where("uid", "==", uid)
            .get()
            .then(snapshot => {
              return snapshot.forEach(doc => {
                batch.delete(doc.ref);
              });
            })
            .catch(err => {
              console.log(err);
            })
        )

        .then(
          db
            .collection("posts")
            .doc(post_id)
            .get()
            .then(snap => {
              return batch.delete(snap.ref);
            })
            .then(batch.commit())
        )
        .catch(err => {
          console.log(err);
        });`

【问题讨论】:

    标签: node.js firebase transactions google-cloud-firestore google-cloud-functions


    【解决方案1】:

    确保从您的 then 函数返回 Promise,并最终从您的云函数返回 Promise。 ESLint 是捕捉此类错误的绝佳工具。


      let batch = db.batch();
      return db.collection("myposts").doc(post_id)
        .collection("fun").get()
        .then(snapshot => {
          snapshot.forEach(doc => {
            batch.delete(doc.ref);
          });
    
          return null;
        })
    
        .then(() => {
          return db.collection("relations_new")
            .where("uid", "==", uid)
            .get();
        })
    
        .then(snapshot => {
          snapshot.forEach(doc => {
            batch.delete(doc.ref);
          });
    
          return null;
        })
    
        .then(() => {
          return db.collection("relation")
            .where("uid", "==", uid)
            .get();
        })
    
        .then(snapshot => {
          snapshot.forEach(doc => {
            batch.delete(doc.ref);
          });
    
          return null;
        })
    
        .then(() => {
          return db.collection("posts").doc(post_id).get();
        })
    
        .then(snap => {
          batch.delete(snap.ref);
          return null;
        })
    
        .then(() => {
          return batch.commit();
        })
    
        .then(() => {
          console.log("Success");
          return null;
        })
    
        .catch(err => {
          console.log(err);
        });
    

    【讨论】:

    • 我正在使用异步/等待,在我的情况下,当我将“异步”与批量写入一起使用时会发生错误,但忘记在函数外部添加“等待”因此批量提交发生在之前批量写入。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 2020-08-21
    • 2022-11-26
    • 1970-01-01
    相关资源
    最近更新 更多