Firestore 中的查询删除正在删除所有内容,如何解决?

Query delete in Firestore is deleting everything, how to fix it?

我需要从集合中删除所有文档,但我应该只删除 'pedido' 等于 'this.pProdutos' 的文档。问题是,即使在查询之后,它也会删除整个集合中的所有文档。 我现在正在使用下面的代码:

    this.db.collection('produtosPedidos', ref => ref.where('pedido', '==', this.pProdutos)).ref
    .get()
    .then(querySnapshot => {
      querySnapshot.forEach((doc) => {
        doc.ref.delete().then(() => {
          console.log("Document successfully deleted!");
        }).catch(function(error) {
          console.error("Error removing document: ", error);
        });
      });
    })
    .catch(function(error) {
      console.log("Error getting documents: ", error);
    });

问题是由您在此处查询末尾的尾随 .ref 引起的:

ref.where('pedido', '==', this.pProdutos)).ref

第一部分 ref.where('pedido', '==', this.pProdutos)) 构造一个查询,然后调用 ref 对该查询 returns 一个 CollectionReference 到整个集合。删除尾随的 .ref 它应该可以工作。

this.db.collection('produtosPedidos', ref => ref.where('pedido', '==', this.pProdutos))
    .get()
    .then(querySnapshot => {
        ...

对于这种类型的操作,运行 它通过 AngularFire 没有额外的好处。我建议在裸 JavaScript SDK 上简单地 运行 它,以减少代码。由于 AngularFire 是建立在 JavaScript SDK 之上的,因此当您执行此操作时,两者可以完美地互操作。

在代码中:

firebase.firestore().collection('produtosPedidos').where('pedido', '==', this.pProdutos)
    .get()
    .then(querySnapshot => {

  deleteOffice(office) {
    this.firestore
      .collection('offices', (ref) => ref.where('name', '==', office.name))
      .get()
      .subscribe((querySnapshot) => {
        querySnapshot.forEach((doc) => {
          doc.ref
            .delete()
            .then(() => {
              console.log('Document successfully deleted!');
            })
            .catch(function (error) {
              console.error('Error removing document: ', error);
            });
        });
      });