TheUnreal
TheUnreal

Reputation: 24492

Nested observables subscription

I have the following code:

getUsers() {
    this.af.database.list(DB_URL)
    .map((users) => {
      return users.map((user) => {
        user.data = this.af.database.object(DB_URL_2).take(1);
        return user;
      });
    })
    .take(1)
    .subscribe((itemPrefs) => {
      this.users = users;
    })
  }

Which returns users array, where user.data is an observable (the user information).

How I can return the data of the user as an object and not as an observable? I tried to subscribe to my user.data but it still returning the observable:

return users.map((user) => {
            user.data = this.af.database.object(DB_URL_2).take(1)
.subscribe((data) => {
return data;
});
        return user;
      });

Upvotes: 0

Views: 906

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136194

I think selectMany should do the trick, each inner function of selectMany(RxJS4) mergeMap(RxJS 5) would to return observable that will flatten. Which means that it will make sure that all inner data would be flatten.

Code

getUsers() {
    this.af.database.list(DB_URL)
    .mergeMap((user, i) => {
        return this.af.database.object(DB_URL_2).take(1).map(data => {
           user.data = data;
           return user;
        });
      });
    })
    .take(1)
    .subscribe((itemPrefs) => {
      this.users = users;
    })
}

Upvotes: 1

Related Questions