首页 > 解决方案 > 编写 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"));
}

请建议一种有效地在循环内进行批量删除的方法。提前致谢!

标签: javascriptfirebasegoogle-cloud-firestore

解决方案


没有看到你所有的代码,很难确切地知道它是如何失败的或者如何让它变得高效。我已经对你的代码进行了一些编辑,这应该可以工作:问题在于 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"));
}

推荐阅读