Angular: How to use result of `.subscribe()` in consequent Observable

Viewed 1696

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);
});
3 Answers

Try flatMap:

 this.serviceA.getOrg()
    .flatMap(org => this.serviceB.getOrgType(org.id))
    .subscribe(type=> {
      console.log(type);
    });

try like this :

this.serviceA.getOrg()
    .flatMap((org) => {
        console.log('org', org);
        return this.serviceB.getOrgType(org.id)
    })
    .subscribe((type) => {
        console.log('type', type);
    });

Try using .flatMap() instead:

this.serviceA.getOrg()
    .flatMap((org) => this.serviceB.getOrgType(org.id))
    .subscribe((type) => {
        console.log(type);
});
Related