I'm unable to find a clean way of fetching nested data the rxjs way.
There's an observable returning all users and an observable returning all posts given a user id.
getUsers(): Observable<User[]>
and
getPosts(id: string): Observable<[]>
How would I return a list of users including their posts?
At the moment I prepare the data using 2 nested subscriptions.
nestedFetch() {
this.service.getUsers()
.subscribe(
res => {
res.forEach((user) => {
this.service.getPosts(user.Id).subscribe(
posts => {
user.posts = posts;
this.users.push(user);
},
err => console.log('something went wrong: ' + err)
)
})
},
err => console.log('something went wrong: ' + err)
);
}
I would rather use operators something like this. But how can i return users including their posts?
nestedFetch() {
this.service.getUsers().pipe(
map(users => users.map(u => this.membership.getPosts(u.id))),
mergeMap(posts => forkJoin(posts))
).subscribe(
res => this.posts = posts, //returns only posts but not users
err => console.log('something went wrong: ' + err)
)
}