Gayathri
Gayathri

Reputation: 1906

Angular resolve with nested API Calls

The procedure of below code is to fetch email id from an api and resolve with details of that email id. I am in need to return the details directly as a response instead of needing to subscribe since I have just have this.route.snapshot.data.value assigned to a component and it takes data rightaway. Is there a way to achieve it? Please favour. I haven't written nested ones previously in resolve so , please guide. I just gave a try as below but it doesn't satisfy the need.

  resolve() {


        let info = environment.DETAIL_URL;

        return this.appService.getInfo(info).pipe(map((res) => {
            let user: any = res;
            console.log(res);
            let params: any = {};
            params.personID = user.emailID;
            return this.appService.getDataFromParams(environment.DATA_URL, params).pipe(map((res) => {
                return res;
            }))
        }))
    }

Upvotes: 1

Views: 345

Answers (2)

D Pro
D Pro

Reputation: 1786

use mergeMap or switchMap instead of map to return data from the inner observable. there's also exhaustMap and concatMap, in case you want to know them all - they all have different behavior

  resolve() {


        let info = environment.DETAIL_URL;

        return this.appService.getInfo(info).pipe(mergeMap((res) => {
            let user: any = res;
            console.log(res);
            let params: any = {};
            params.personID = user.emailID;
            return this.appService.getDataFromParams(environment.DATA_URL, params).pipe(map((res) => {
                return res;
            }))
        }))
    }

Upvotes: 0

Jeff Mercado
Jeff Mercado

Reputation: 134601

Use switchMap() instead of map().

return this.appService.getInfo(info).pipe(
    switchMap(user =>
        this.appService.getDataFromParams(environment.DATA_URL, {
            personID: user.emailID
        })
    )
})

Upvotes: 1

Related Questions