Reputation: 16618
Angular2: I want to use the result of .subscribe()
in a consequent observable.
This is in order to use the id from the parent subscribe in the nested subscribe.
I've tried using .switchMap()
first, but this doesn't seem to work.
This is my attempt:
this.serviceA.getOrg()
.switchMap(org => this.serviceB.getOrgType(org.id))
.subscribe(type => {
console.log(type);
});
Upvotes: 4
Views: 1825
Reputation: 2633
Try using .flatMap()
instead:
this.serviceA.getOrg()
.flatMap((org) => this.serviceB.getOrgType(org.id))
.subscribe((type) => {
console.log(type);
});
Upvotes: 2
Reputation: 11184
try like this :
this.serviceA.getOrg()
.flatMap((org) => {
console.log('org', org);
return this.serviceB.getOrgType(org.id)
})
.subscribe((type) => {
console.log('type', type);
});
Upvotes: 3
Reputation: 13346
Try flatMap:
this.serviceA.getOrg()
.flatMap(org => this.serviceB.getOrgType(org.id))
.subscribe(type=> {
console.log(type);
});
Upvotes: 4