我正在尝试从我的集合中删除许多具有特定categoryId值的文档,但我认为这样做的方式错误 .

async deleteCol(id: string) {
    const cars: firebase.firestore.QuerySnapshot 
      = await this.db.collection('cars', ref => ref.where('categoryId', '==', id)).ref.get();
    const batch = this.db.firestore.batch();

    cars.forEach(car => {
      batch.delete(car);
    });

    batch.commit();
  }

有两个问题:

  • 打字稿在 batch.delete(car); 中显示汽车错误

“QueryDocumentSnapshot”类型的参数不能分配给“DocumentReference”类型的参数 . “QueryDocumentSnapshot”类型中缺少属性“firestore” .

  • 如果有例如两辆汽车并且每辆汽车都有不同的categoryId,那么 forEach 会被触发两次(对于每个文档,不是针对具有特定categoryId的文档),但应该只有一次或者可能有更好更简单的方法来删除所有文档具体情况如何?

更新:

好的,所以这个版本正在运行:)

public async deleteCol(id: string): Promise<void> {
    const carsList: Observable<firestore.QuerySnapshot> = await this.db.collection('cars', ref => ref.where('categoryId', '==', id)).get();
    const batch = this.db.firestore.batch();
    carsList.pipe(
      mergeMap(cars => cars.docs),
      map((car: QueryDocumentSnapshot) => batch.delete(car.ref))
    ).toPromise().then(() => batch.commit());
  }