jbb
jbb

Reputation: 550

observable list angular 2

I'm new to Rx and have problems to get what i want. please help: i want to get a list of User Profiles Ids for a List of UserIds I have a problem merging all the results to one Observable

getListOfUsers(): Observable< Profile[]> {
  let users = this.af.database.list('listOfUsers/');

  let singleuser = users.flatMap(
    (users)=> {
      console.log(users);
      return users.map( singleuser => {
        return singleuser;
      })
    } 
  );


  return singleuser.flatMap(
  (profile) => {
    return this.af.database.object('profiles/' + profile['$key']).map(
      singleprofile => {
        console.log(singleprofile);
        return singleprofile;
      }
    );
  });
}

This function gives me back single Profile Objects, how can i merge all of them to one list of Profile Objects? I tryed to wrap the return function with Observable.forkjoin and some other stuff but nothing worked for me. Is there any option to use the whole list in the second call instead of splitting it to single users first? Please help

Upvotes: 0

Views: 744

Answers (2)

Mark van Straten
Mark van Straten

Reputation: 9425

If you want your function to return an Observable<UserProfile[]> instead of Observable<UserProfile> then you need to add .toArray() so all emissions are put into one array emission:

return singleuser.flatMap(
  profile => this.af.database.object('profiles/' + profile['$key'])
)
.toArray();

Upvotes: 0

Michael Kang
Michael Kang

Reputation: 52867

I think this is what you want:

let users = this.af.database.list('listOfUsers/');
var profiles = users.flatMap(t => 
        Observable.forkJoin(
             t.map(x => this.af.database.object('profiles/' + x.uid))
        )
    );
return profiles;

Upvotes: 1

Related Questions