【问题标题】:Angularfire2 + Firestore query result with referenceAngularfire2 + Firestore 查询结果与参考
【发布时间】:2018-07-07 13:59:56
【问题描述】:

我的集合中有几百个文档。其中很少有人损坏,我想删除它们。我知道如何找到损坏的文档,并通过查询获得它们。但它只是一个数据,没有文档 ID 或任何东西。

所以我的问题是,如何删除我在查询中收到的文档?或者还有其他方法如何根据某些属性删除文档?

getData(target) {
    return this.afs.collection('someCollection', ref => {
      let query: firebase.firestore.CollectionReference | firebase.firestore.Query = ref;
      query = query.where('label', '==', target);
      return query;
    });
  }

this.dataService.getData('CorruptedLabel').valueChanges().subscribe(resp => {
      console.log('resp', resp); // Here I get and array of objects
      // I would like to go through that array and call delete() on each item
    });

【问题讨论】:

    标签: angularfire2 google-cloud-firestore


    【解决方案1】:

    您可以获取firestore文档参考并删除文档

    this.dataService.getData('CorruptedLabel').snapshotChanges().subscribe(snapshots
    => {
      snapshots.forEach(snapshot => {
         if(snapshot){
              this.afs.collection('someCollection').doc(snapshot.payload.doc.id).delete();
          }
      }
    });
    

    【讨论】:

    • 您好,谢谢您的回答。但它什么也没做。请问我应该更改 getData 方法中的某些内容吗? snapshotChanges 部分没有被触发。
    • 如果你用.valueChanges().subscribe() 得到正确的值应该没问题
    • 使用我的原始代码,我得到了一个包含 4 个对象的数组,这正是我应该做的。使用您的代码,我什么也得不到。我已将 console.log 插入到地图功能中,但未显示。而且我在网络选项卡中看不到任何活动。好像什么都没做。
    • firestore 必须有一个 id 以及任何文档,可能您使用 .valueChanges 返回它没有显示的数据。使用我的回答中的.snapshotChanges(),您可以做到.doc(snapshot.payload.doc.id).delete()
    • 是的,id 丢失了,我已经添加了它,我看到你也添加了它。非常感谢,现在可以使用了。
    猜你喜欢
    • 2019-01-16
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 2021-09-11
    • 2021-04-19
    相关资源
    最近更新 更多