I am having some troubles making nested Observable calls. By that I mean a call to a http service that retrieve a user, then getting the id from the user to make another htt
You should read up on rxjs's operators a little. Your examples are very verbose and use flatMap
and map
in a way they're not supposed to be used. Also your first example can't work, because you're not subscribing to the Observable.
This will do what you need:
ngOnInit() {
this.userService.getUser()
.do(u => this.user = u) //.do just invokes the function. does not manipulate the stream, return value is ignored.
.flatMap(u => this.userService.getPreferences(u.username))
.subscribe(p => this.preferences = p);
}
As of rxjs 5.5 you should use the pipeable operators:
ngOnInit() {
this.userService.getUser().pipe(
tap(u => this.user = u),
flatMap(u => this.userService.getPreferences(u.username))
).subscribe(p => this.preferences = p);
}