0

So I am trying to use batched writes in my firebase function to batch my writes in batches of 500. However for some reason I am getting the error "Error: Cannot modify a WriteBatch that has been committed." and can't seem to spot what I am doing wrong. It is storing the first few results but after that it is giving me the error in my cloud functions log. Any suggestions would be appreciated =D


          messages.forEach((item, index) => {
            var all = _.find(item.parts, { which: "" });
            var id = item.attributes.uid;
            var idHeader = "Imap-Id: " + id + "\r\n";
            // eslint-disable-next-line handle-callback-err
            simpleParser(idHeader + all.body, (err, mail) => {
              // access to the whole mail object
              console.log(mail.subject);
              console.log(mail.html);

              let newDate = mail.date.valueOf();

              batch.set(
                docRef,
                {
                  Emails: admin.firestore.FieldValue.arrayUnion(
                    JSON.stringify({
                      subject: mail.subject,
                      body: mail.text,
                      date: newDate,
                      from: mail.from,
                    })
                  ),
                  //....
                },
                { merge: true }
              );
              if (
                (index % 500 === 0 && index > 0) ||
                index === messages.length - 1
              ) {
                return batch.commit().then(() => {
                  return console.log("SUCCESS");
                });
              }
            });
          });


CodingIsFun33
  • 433
  • 3
  • 14
  • Not sure what else I can add, thats the entire cloud function. That has the all the batched write stuff I added. There is nothing else to show lol. – CodingIsFun33 May 06 '20 at 21:15
  • The whole thing matters tho, or so I thought in this case. I removed some of the extra stuff and just left the part where the issue is. Although I feel like now its even harder to tell whats going on and where the issue is lol. Maybe someone can spot it now. I legit can't see where I am messing up lol and it seems nobody else can either haha. It should not be this hard to do batched writes =[ – CodingIsFun33 May 06 '20 at 22:02

1 Answers1

3

A batch process needs to have two parts, a main function and a callback from each batch. The issue is you are trying to redeclare the batch process once its been commited for the first batch. create a new batch or seperate it in a specific function like the documentation example.

Here are some resources to help:

https://medium.com/@michael.kimpton/batch-processing-with-firebase-cloud-functions-aa11640cc9ac

How to do a bulk update in Firestore

Batch write to firebase cloud firestore

DIGI Byte
  • 4,225
  • 1
  • 12
  • 20