使用userId获取用户信息-Angular Firestore [英] Get user info with userId - Angular Firestore
本文介绍了使用userId获取用户信息-Angular Firestore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个不同的收藏集:
I've got 2 different collections:
- 用户:姓名,电子邮件和头像(来自其社交网络)
- 配方:用户可以创建的文档.它还包含
userId
及其对先前集合的引用
- Users: name, email and avatar (taken from their social network)
- Recipes: documents that the user can create. It also contains
userId
with their references to previous collection
要在我的页面上显示这些食谱,我使用:
To display these recipes on my page I use:
this.recipes = collection.snapshotChanges().map(actions => {
return actions.map(action => ({
$id: action.payload.doc.id,
...action.payload.doc.data()
}));
});
并且:
<ion-card *ngFor="let recipe of recipes | async; let i = index">
...
同时获得用户头像和姓名的最佳方法是什么?
What's the best way to get also the user avatar and name?
推荐答案
尝试此代码
this.recipes = collection.snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data();
const id = a.payload.doc.id;
//this.afs.collection('users') is the ref to users collection, yours should be different
return this.afs.collection('users').doc(data.userId).snapshotChanges().map(userActions=>{
return userActions.payload.data();
}).map(user=>{
return { id:id, userName:user.name, ...data };
})
});
}).flatMap(merge => Observable.combineLatest(merge)).map(data => {
return [].concat(...data).map(d => {
return d;
})
});
这篇关于使用userId获取用户信息-Angular Firestore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文