【问题标题】:Getting a single AngularFirestoreDocument from a AngularFirestoreCollection query从 AngularFirestoreCollection 查询中获取单个 AngularFirestoreDocument
【发布时间】:2018-06-18 15:56:59
【问题描述】:

在 Firestore 中,我的 Vendor 文档中有一个 uid 作为字段:

/vendors
    +doc1
        uid: 1
    +doc2
        uid: 2

为了访问供应商,我使用以下函数:

getVendor(index: string): AngularFirestoreDocument<Vendor> {
    return afs.doc<Vendor>(`vendors/${index}`);
}

我想创建一个类似的函数,它也返回一个AngularFirestoreDocument,但该函数需要一个uid 来获取文档:

getVendorByUID(uid: string): AngularFirestoreDocument<Vendor> {

    ...

    return theDoc;
}

似乎我必须查询 AngularFirestoreCollection,但不知道如何从集合中提取文档。

【问题讨论】:

    标签: firebase google-cloud-firestore angularfire2 angularfire5


    【解决方案1】:

    我建议使用 uid 作为记录的键;这样你就可以通过 URL w/getVendor

    引用它

    否则需要查询:

    // Query the first vendor where the uid matches
    afs.collection<Vendor>(`vendors`, ref => ref.where('uid', '==', uid).limit(1))
      .valueChanges()
      .pipe(
        map(vendors => vendors[0]) // flatten the result
      );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-13
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 2018-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多