从 Firestore 集合中引用的文档访问数据 [英] access data from document referenced inside firestore collection

查看:28
本文介绍了从 Firestore 集合中引用的文档访问数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

firstCollection 文档示例

<代码>{name: '父母',//更多字段,第二个://从第二个集合引用文档}

secondCollection 引用的文档示例

<代码>{title: '文档标题',更多:3456,等等: '...'}

我有包含 angularfire2 库的 angular 应用程序.在我的一个 html 文件中,我需要打印第一个集合中的数据,包括第二个集合的文档中的一些字段值.第二个集合中的文档具有来自第一个集合的有效引用(见上文).这就是我想要做的:

这有效 - {{first.name}}现在的问题是,如何从这里的第二个集合访问文档数据?我需要的是以下内容:{{first.second.title}} - 这不起作用,因为 'second' 只是参考

解决方案

从 Cloud Firestore 读取数据很浅.这意味着当您阅读第一个文档时,您还没有加载第二个文档(您有引用)中的任何数据.

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

I have a collection inside firestore database that consists of several data fields and references. Structure looks like this:

firstCollection document sample

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

secondCollection referenced document sample

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

I have angular application with angularfire2 library included. In one of my html files I need to print data from the first collection including some field values from document of the second collection. Document from the second collection has valid reference from the first collection (see above). This is what I want to do:

<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>

解决方案

Reading data from Cloud Firestore is shallow. This means when you read the first document, you have not yet loaded any of the data in the second document (to which you have a reference).

You'll have to perform another get() or listen operation on that reference in order to get the data. https://github.com/angular/angularfire2/blob/master/docs/firestore/documents.md

这篇关于从 Firestore 集合中引用的文档访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆