从 firestore 集合中引用的文档访问数据

2024-04-22

我在 firestore 数据库中有一个集合,其中包含多个数据字段和引用。结构如下:

FirstCollection 文档样本

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

secondaryCollection 参考文档样本

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

我有 Angular 应用程序,其中包含 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>

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 firestore 集合中引用的文档访问数据 的相关文章

随机推荐