0

I have an array variable named users. it contains users id elements.

users=[uid1, uid2, uid3];

and I want to map this array and get users info from Firestore like this:

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?

2
  • Is that your actual code? And please share your template too. And what gives this error? Commented Dec 13, 2019 at 18:51
  • The code presented has a number of syntax errors. Commented Dec 13, 2019 at 18:59

2 Answers 2

1

thaks @Moxxi I ca run my codes with helps. this my codes; forkJoin not work so I used combineLatest instead of it. anf filter(Boolean) worked with pipe.

 this.usersInfo$= combineLatest(
          users.map((user:any)=>this.afs.doc(`users/${user.uid}`).valueChanges()
        )).pipe(map(x=>x.filter(Boolean)))
Sign up to request clarification or add additional context in comments.

Comments

0

You can map your id's to an array of observable and execute them with an combination operator liek forkJoin or 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);

3 Comments

Answers with explanation of code presented typically get upvoted more often that those which simply present code.
@HereticMonkey exactly bro
This codes are presented. I though I can easily explain what I do. With this way.Thanks for help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.