我有3个收藏集:
现在我需要获取用户详细信息的用户详细信息
我的代码是:
return this.dbCollectionService.userOrganizationCollection.snapshotChanges()
.pipe(map(res => {
return res.map(data => {
var userData = data.payload.doc.data()
let userDetails : any;
if (userData.userDetails) {
this.angularFirestore.doc(userData.userDetails).valueChanges().subscribe(res => {
userDetails = res;
console.log(userDetails);
});
return { id, ...userDetails }
}).filter( f => f.organizationId != undefined && f.organizationId == orgId)
}));
这能够正确控制结果,但不会返回数据。
可能需要花费一些时间从数据引用中获取数据,并且return语句会更早执行。
答案 0 :(得分:0)
在return { id, ...userDetails }
上进行预订之前,将执行this.angularFirestore
.doc(userData.userDetails).valueChanges()
。
订阅内的订阅不是一个好主意,您应该使用其他可插拔的运算符(如switchMap)来链接两个调用:
return this.dbCollectionService
.userOrganizationCollection.snapshotChanges()
.pipe(
map(res => res.map(data => data.payload.doc.data())),
map(userDatas => userDatas.filter(_ => _)),
map(userDatas => userDatas.map(this.angularFirestore.doc(userData.userDetails).valueChanges()
.pipe(map(userDetails => ({ id, ...userDetails }))))),
switchMap(userDatas$ => combineLatest(userDatas$)),
map(users => users.filter( f => f.organizationId != undefined && f.organizationId == orgId))
);