AngularFire2查询未返回文档ID

时间:2019-03-14 18:12:18

标签: angular firebase google-cloud-firestore angularfire2

我在服务中使用AngularFire2从Firestore集合中获取数据。代码看起来像这样:

this.db.collection('organizations')
  .valueChanges()
  .pipe(first())
  .toPromise()
  .next(organization => console.log(organization));

控制台正在按预期方式记录组织对象。但是该对象缺少Firestore文档的ID。

所以我想知道是否可以做一些事情来获取ID作为查询的一部分...

1 个答案:

答案 0 :(得分:1)

您可以使用类似于以下的快照:

long_filtered <- long %>%
  mutate(id = as.character(id)) %>%
  semi_join(CleanAPT %>%
              mutate(id = as.character(id)),
            by = "id")

有关private getOrganizations(whereClause: any): any { return this.db.collection('organizations') .snapshotChanges() .pipe( map((docs: any) => { return docs.map(a => { const data = a.payload.doc.data(); const id = a.payload.doc.id; return {id, ...data}; }); }) ); } 的更多详细信息,请检查以下内容:

https://github.com/angular/angularfire2/blob/master/docs/firestore/documents.md#snapshotchanges

  

snapshotChanges()

     

这是什么?-作为DocumentChangeAction返回一个Observable数据。

     

为什么要使用它?-当您需要文档数据但又需要   保持元数据。此元数据为您提供了底层样式   DocumentReference和 文档ID 。拥有文件ID   使使用数据操作方法更加容易。这种方法给   您可以通过其他Angular集成(例如ngrx)获得更大的功能,   表格和动画(归因于type属性)。的type属性   每个DocumentChangeAction对于ngrx缩减器,表单状态,   和动画状态。这是什么? -将数据的Observable返回为

相关问题