[英]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.