[英]how to execute observable in array Angular?
我有一個名為users
的array
變量。 它包含用戶 ID 元素。
users=[uid1, uid2, uid3];
我想映射這個array
並像這樣從Firestore獲取用戶信息:
let userInfo;
userInfo = users.map((element:any) => {
if (element){
this.afsService.doc(`user/${element}`).subscribe(user => {
return {...user}
});
}
...
當這段代碼起作用時,我收到了這個錯誤:
You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
所以我的問題; 如何在array
迭代中執行Observable
?
謝謝@Moxxi 我可以在幫助下運行我的代碼。 這是我的代碼; forkJoin 不起作用,所以我使用 combineLatest 代替它。 anf filter(Boolean) 與管道一起使用。
this.usersInfo$= combineLatest(
users.map((user:any)=>this.afs.doc(`users/${user.uid}`).valueChanges()
)).pipe(map(x=>x.filter(Boolean)))
您可以將您的 id 映射到一個可觀察數組,並使用組合運算符 liek forkJoin 或 combineLatest 來執行它們。 訂閱將為您帶來一系列結果
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.