我正试图从我的收藏夹中删除许多具有特定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'类型的参数。属性'firestore'在'QueryDocumentSnapshot'类型中丢失。
例如,如果有两辆汽车,每辆汽车具有不同的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());
}