从 AngularFirestoreCollection 查询中获取单个 AngularFirestoreDocument

Getting a single AngularFirestoreDocument from a AngularFirestoreCollection query

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

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

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

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

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

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

    ...

    return theDoc;
}

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

我建议使用 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
  );