简体   繁体   English

AngularFirestore:检索集合中的文档引用并将 map 检索到 class

[英]AngularFirestore: retrieving a document reference in a collection and map it to a class

I want to combine two firestore collections that are linked with an reference field.我想组合两个与参考字段链接的 Firestore collections。

I have these two collections:我有这两个 collections: 在此处输入图像描述

I want to display the displayName of the referenced user with a single userstory instead of the id of an user.我想用单个用户故事而不是用户的 id 显示被引用用户的 displayName。

Template:模板:

<tbody id="project">
   <tr *ngFor="let userstory of userstories | async">
      <td>{{ userstory.name }}</td>
      <td>{{ userstory.description }}</td>
      <td>{{ userstory.status }}</td>  
      <td>{{ userstory.points }}</td>
      <td>{{ userstory.ownerobj.displayName }}</td> <!-- I want to display to displayName of the here -->
      <td>{{ userstory.uid }}</td>
   </tr>
</tbody>

Model: Model:

export class Userstory {
  public uid: string;
  public name: string;
  public description: string;
  public status: number;
  public points: number;
  public owner: DocumentReference;
  public ownerobj: User;
}  

export class User {
  uid: string;
  email: string;
  displayName: string;
  photoURL: string;
  emailVerified: boolean;
}

Service:服务:

getUserstories() {
return this.afs.collection<Userstory>('userstories')
  .snapshotChanges()
  .pipe(
    map(res => res.map(el => {
      let userstory = el.payload.doc.data() as Userstory;
      userstory.uid = el.payload.doc.id;
      this.afs.doc(`/users/${userstory.owner.id}`).snapshotChanges().pipe(map(item => {
        userstory.ownerobj = item.payload.data() as User
      }));
      return userstory;
    })),
  )
};

Obviusly the code above doesn't work because it is not an synchronous function.显然上面的代码不起作用,因为它不是同步的 function。 But how can I get this to work to combine the user with an userstory?但是我怎样才能让这个工作将用户与用户故事结合起来呢?

I'm trying to learn firestore but there isn't much information on how to do this.我正在尝试学习firestore,但没有太多关于如何做到这一点的信息。 Any help is appreciated.任何帮助表示赞赏。

What I would recommend, is that you do not try to use a non-relational database as if it were a relational one, in the Owner property you must put all the necessary owner information.我建议您不要尝试使用非关系数据库,就好像它是关系数据库一样,您必须在 Owner 属性中放置所有必要的所有者信息。

If you don't want to work so then the most you can do is create a function that calls the owner's document and displays its data.如果您不想工作,那么您最多可以创建一个调用所有者文档并显示其数据的 function。

But in itself, a relatio is not posible.但就其本身而言,关系是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 AngularFireStore 将文档/集合返回为 object - Return document/collection as object using AngularFireStore 带有子查询的AngularFireStore返回集合 - AngularFireStore return collection with subquery 基于angularfirestore集合中的子集合的字段值进行搜索 - Search based on a field value of subcollection in angularfirestore collection 通过快照更改将HTML与AngularFirestore集合绑定 - Bind HTML with AngularFirestore collection via snapshotChanges 使用AngularFirestore Collection orderBy和snapShotChanges方法 - Using AngularFirestore Collection orderBy with snapShotChanges method 如何从另一个文档angularId的Id获取文档 - How to get a document from Id gotten from another document angularFirestore 在使用“where()”时,在AngularFirestore中查询集合时不会产生任何数据 - Querying Collection in AngularFirestore results in no data when using “where()” 无法读取collection.set()上未定义的属性afs(Angularfirestore) - Cannot read propery afs (Angularfirestore) of undefined on collection.set() Map 文档参考 Object 与 AngularFire 中的 SnapshotChanges - Map Document Reference In Object with SnapshotChanges in AngularFire 有没有办法将从子集合 Firebase 中检索到的文档存储到一个数组中以显示其信息? AngularFirestore - Is there a way to store the retrieved documents from sub-collection Firebase into an array for displaying its info? AngularFirestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM