4

I am deploying this code on cloud functions, getting Cannot modify a WriteBatch that has been committed, I have tried committing after getting every collection but it's not a right way and is inconsistent, could not spot the error after trying several hours. Also the code run on first time after cold start , this post has same problem Batch write to firebase cloud firestore , where to create

a new batch for each set of writes. in this code .

 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);
        });`
gautam
  • 1,701
  • 2
  • 17
  • 33

1 Answers1

6

Make sure to return promises from your then functions, and to ultimately return a promise from your cloud function. ESLint is a great tool for catching these kinds of errors.


  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);
    });
James Poag
  • 2,320
  • 1
  • 13
  • 20
  • 2
    I'm using async/await, in my case the error occur when i'm using 'async' with the batch write, but forgotten to add 'await' outside the function thus batch commit happen before batch write. – morph85 Jul 02 '19 at 11:30