2017-10-10 81 views
1

我有一个firestore数据库内的集合,它由几个数据字段和引用组成。结构是这样的:从firestore集合中引用的文档访问数据

firstCollection文档样张

{ 
    name: 'parent', 
    //more fields, 
    second: //reference to document from second collection 
} 

secondCollection引用文档样本

{ 
    title: 'document title', 
    more: 3456, 
    etc: '...' 
} 

我有angularfire2库角应用包括在内。在我的一个html文件中,我需要从第一个集合中打印数据,包括来自第二个集合文档的一些字段值。来自第二个集合的文档具有来自第一个集合的有效参考(参见上文)。这是我想要做的:

<div *ngFor="let first of firstCollection | async"> 

    this works - {{first.name}} 

    Now the question is, How can I access document data from the second collection here? 
    What I need is something like the following: 

    {{first.second.title}} - this doen't work because 'second' is just reference 

</div> 

回答

1

从Cloud Firestore读取数据很浅。这意味着当您阅读第一个文档时,您尚未加载第二个文档中的任何数据(您有参考)。

您必须执行另一个get()或侦听该引用的操作才能获取数据。 https://github.com/angular/angularfire2/blob/master/docs/firestore/documents.md

+1

非常感谢您的回复。我希望有内建的东西可以帮助在需要时使用引用的文档进行操作。我认为我将从参考文档中去规范化收集和复制字段,而不是对每个“firstCollection”文档执行“get”操作。 –