javascript - 编写 Firebase 批处理函数以删除数组中的数据
问题描述
我已经实现了以下使用批处理删除数据的功能。但这给了我一个未处理的异常,说“无法修改已提交的 WriteBatch。我认为这是因为我正在使用的 for 循环。
data.forEach(async (element: any) => {
const snapshot = await db
.collection("car")
.doc(element.brand)
.collection("model")
.doc(element.model)
.collection("year")
.orderBy("year")
.get();
snapshot.forEach(async (doc) => {
if (element.service === "Car Spa") {
console.log("Cond1")
const ref1 = db
.collection(element.service)
.doc(element.sub_service)
.collection("Body Type")
.doc(element.bodyType)
.collection("model")
.doc(element.model)
.collection("mechanic")
.doc("email@gmail.com")
batch.delete(ref1);
operationCounter++;
} else {
console.log("Cond:2")
const ref2 = db
.collection(element.service)
.doc(element.sub_service)
.collection("vehicle")
.doc(element.brand)
.collection("model")
.doc(element.model)
.collection("year")
.doc(doc.data().year.toString())
.collection("mechanic")
.doc("email@gmail.com")
batch.delete(ref2)
operationCounter++;
}
let _id =
element.service +
element.sub_service +
element.brand +
element.model +
doc.id;
const ref3 = db
.collection("mechanics")
.doc("email@gmail.com")
.collection("services")
.doc(_id)
batch.delete(ref3);
operationCounter++;
if (operationCounter === 500) {
console.log("MORE THAN 500");
batches.push(batch.commit());
batch = db.batch();
operationCounter = 0;
}
});
});
if (batches.length === 0) {
await batch.commit().then(() => console.log("DONE ONE BAtch"));
} else {
await Promise.all(batches).then(() => console.log("DONE"));
}
请建议一种有效地在循环内进行批量删除的方法。提前致谢!
解决方案
没有看到你所有的代码,很难确切地知道它是如何失败的或者如何让它变得高效。我已经对你的代码进行了一些编辑,这应该可以工作:问题在于 foreach 看到这个问题,使用 async/await 和 forEach 循环
这是正确的 forEach 循环
let batch = db.firestore().batch()
for (const element of data.docs) {
const snapshot = await db
.collection("car")
.doc(element.brand)
.collection("model")
.doc(element.model)
.collection("year")
.orderBy("year")
.get();
for( const doc of snapshot.docs) {
if (element.service === "Car Spa") {
const ref1 = db
.collection(element.service)
.doc(element.sub_service)
.collection("Body Type")
.doc(element.bodyType)
.collection("model")
.doc(element.model)
.collection("mechanic")
.doc("email@gmail.com")
batch.delete(ref1);
operationCounter++;
} else {
const ref2 = db
.collection(element.service)
.doc(element.sub_service)
.collection("vehicle")
.doc(element.brand)
.collection("model")
.doc(element.model)
.collection("year")
.doc(doc.data().year.toString())
.collection("mechanic")
.doc("email@gmail.com")
batch.delete(ref2)
operationCounter++;
}
let _id =
element.service +
element.sub_service +
element.brand +
element.model +
doc.id;
const ref3 = db
.collection("mechanics")
.doc("email@gmail.com")
.collection("services")
.doc(_id)
batch.delete(ref3);
operationCounter++;
if (operationCounter === 500) {
await batch.commit();
batch = db.batch();
operationCounter = 0;
}
}
}
if (batch.length === 0) {
await batch.commit().then(() => console.log("DONE LAST BATCH"));
}
推荐阅读
- excel-2007 - Excel 2019 与 Excel 2007:数据验证?
- sql - 分组并求和,然后根据 SQL 中的最大值计算结果
- json - 如何在 ExtJS 中加入模型?
- python - 通过视频通话进行手语翻译不会产生同步翻译
- python - Discord.py TypeError:“int”类型的参数不是可迭代错误
- javascript - 动态输入计算器
- android - 使用 AuroraStore 代码,它如何获取“库”(已安装应用程序的历史记录),以及如何获取它们的安装时间?
- php - 未处理的异常:FormatException:意外字符(在字符 1 处)颤振
- c# - (C#) 从 TextBox WinForms 的删除字符中获取索引
- laravel - Laravel 8:将工厂属性传递给子关系