[英]how to execute observable in array Angular?
I have an array
variable named users
.我有一个名为
users
的array
变量。 it contains users id elements.它包含用户 ID 元素。
users=[uid1, uid2, uid3];
and I want to map this array
and get users info from Firestore like this:我想映射这个
array
并像这样从Firestore获取用户信息:
let userInfo;
userInfo = users.map((element:any) => {
if (element){
this.afsService.doc(`user/${element}`).subscribe(user => {
return {...user}
});
}
...
when this code worked, I got this error:当这段代码起作用时,我收到了这个错误:
You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
so my question;所以我的问题; how to execute
Observable
in array
iterate?如何在
array
迭代中执行Observable
?
thaks @Moxxi I ca run my codes with helps.谢谢@Moxxi 我可以在帮助下运行我的代码。 this my codes;
这是我的代码; forkJoin not work so I used combineLatest instead of it.
forkJoin 不起作用,所以我使用 combineLatest 代替它。 anf filter(Boolean) worked with pipe.
anf filter(Boolean) 与管道一起使用。
this.usersInfo$= combineLatest(
users.map((user:any)=>this.afs.doc(`users/${user.uid}`).valueChanges()
)).pipe(map(x=>x.filter(Boolean)))
You can map your id's to an array of observable and execute them with an combination operator liek forkJoin or combineLatest.您可以将您的 id 映射到一个可观察数组,并使用组合运算符 liek forkJoin 或 combineLatest 来执行它们。 The subscription will bring you an array of the results
订阅将为您带来一系列结果
userIds = [uid1, uid2, uid3];
userInfos$ = forkJoin(userIds.filter(Boolean).map(userId => this.afsService.doc(`user/{userId}`)));
userInfos$.subscribe(console.info);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.