【问题标题】:Trying to fetch child collection of parent document试图获取父文档的子集合
【发布时间】:2018-06-07 18:38:47
【问题描述】:

我在 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;
  }

【问题讨论】:

  • 你能分享你目前拥有的完整代码吗?
  • 我不熟悉 AngularFire。不过,我或许能给你一些指点。当您选择要获取其版本的产品时,将其 ref 传递给您的 getVersionsForProduct 函数,然后扩展它以获取 versions 集合...使用 Web SDK,这将是 product.collection('Versions').get() .希望您可以翻译成 AngularFire(我需要学习。如果我的评论对此不正确,请见谅)

标签: javascript ionic3 google-cloud-firestore angularfire2


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-06
    • 2022-11-16
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 2019-07-10
    • 2023-04-02
    • 2019-11-13
    相关资源
    最近更新 更多