如何从firebase中的``where''查询中获取数据

时间:2018-11-17 17:36:53

标签: angular firebase google-cloud-firestore

我不确定要为this.actions返回的查询对象怎么做

this.actionCollection = this.db.collection('actions');
this.actions = this.actionCollection.ref.where('filterId', '==', this.filterId)

我应该怎么处理this.actions?我尝试将其设置为我的视图,但出现此错误Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'

<h4>Actions List</h4>
<ul>
  <li *ngFor="let action of actions | async">
    {{action | json}}
  </li>
</ul>

1 个答案:

答案 0 :(得分:0)

您需要在其上调用 get 方法。这将返回 firebase.firestore.QuerySnapshot 类型的值。

它本质上是一个Promise,因此您可以将then方法链接到它来获取解析的数据。在这里,尝试一下:

this.actions = this.actionCollection.ref
  .where('filterId', '==', this.filterId)
  .get()
  .then(querySnapshot => {
    querySnapshot.forEach(doc => {
        console.log(doc.id, " => ", doc.data());
    });
  });

查看 Get multiple documents from a collection 作为参考。