1

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;
            }))
        }))
    }
2
  • What is the purpose of .pipe(map((res) => { return res; }))? It does absolutely nothing. Commented Jul 4, 2020 at 11:26
  • originally i do a modification on the response object and return. Commented Jul 4, 2020 at 11:27

2 Answers 2

1

Use switchMap() instead of map().

return this.appService.getInfo(info).pipe(
    switchMap(user =>
        this.appService.getDataFromParams(environment.DATA_URL, {
            personID: user.emailID
        })
    )
})
Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

I actually tried this. for some reason, getInfo gives response in console but getDatafromparams didn't even happen. getDatafromparams internally runs a query and i don't see it hit in console. and response of route.snapshot.data.user be a blank array [ ] .
it is supposed to work. if it doesn't, the problem is elsewhere. please share more code. maybe there's something wrong with getDataFromParams? does it receive proper arguments? add some logging to check or debug
I did a silly mistake hitting a wrong url . and yea, using mergeMap on top works. Thanks for that. Just asking, how'll the same work if 3 API calls are involved like API 1 gives result 1 which is used in API 2 to give result 2 which is inturn used in API 3 and the third API result alone need to be resulted?
it works the same: api1.pipe( switchMap(res1 => api2(res1)), switchMap(res2 => api3(res2)), ).subscribe(res3 => do your stuff)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.