首页 > 解决方案 > 试图获取父文档的子集合

问题描述

我在 Ionic 3 应用程序中使用 AngularFire2 从 Angular Firestore 数据库中获取数据。在数据库中,我有一个“/Products/”集合,其中的每个文档都有一个“/Versions/”子集合:

在此处输入图像描述

在我的应用程序中,我已经获取了产品(转换为 DTO)并想获取它的子版本。我一直在尝试不同的方法,但似乎无法弄清楚如何提及孩子。

这是我使用获取产品的代码:

this.fireStore.collection('/Products/', i => i.orderBy('Title'));

一旦我有了一个Product对象,我如何获取它的版本?

编辑:有人问我到目前为止有什么代码,但如您所见,我缺少“产品”和相关版本之间的链接:

public getVersionsForProduct(product: Product) : AngularFirestoreCollection<Version>{
    var vers = this.fireStore.collection<Version>('/Version/');
    return vers;
  }

标签: javascriptionic3google-cloud-firestoreangularfire2

解决方案


没有看到你所有的相关代码很难说,但你可以得到这样的子集合:

public getVersionsForProduct(product: Product) : AngularFirestoreCollection<Version> {

    const vers = this.fireStore.collection('Products').doc(product.id).collection<Version>('Versions'); 
    return vers;
  }

推荐阅读