尝试在 Firebase 函数中使用批量写入时"Error: Cannot modify a WriteBatch that has been committed."



因此,我尝试在firebase函数中使用批处理写入,以500为一批的方式批处理写入。然而,由于某种原因,我收到了错误"错误:无法修改已提交的WriteBatch。"并且似乎无法发现我做错了什么。它存储了最初的几个结果,但在那之后,它在我的云函数日志中给了我错误。任何建议都将不胜感激=D


messages.forEach((item, index) => {
var all = _.find(item.parts, { which: "" });
var id = item.attributes.uid;
var idHeader = "Imap-Id: " + id + "rn";
// 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");
});
}
});
});

一个批处理过程需要有两个部分,一个主函数和一个来自每个批的回调。问题是,一旦批处理过程被提交给第一批,您就试图重新声明它。创建一个新的批处理或在特定的函数中分离它,如文档示例。

这里有一些资源可以帮助:

https://medium.com/@michael.kimpton/批量处理-具有-firebase-cloud-功能-a11640cc9ac

如何在Firestore 中进行批量更新

批量写入firebase cloud firestore

相关内容

最新更新