2017-10-10 53 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」操作。 –