0

I'm beginner in Angular so I faced a challenge. How to interact with nested http request in Angular using RxJS ?

.pipe(
    map((res): Collection => ({
            id: res.id,
            name: res.name,
            description: res.description,
            tracks: res.tracks.items.map(track => this.trackService.fetchTrack(track.id)) //?
        }) 
    ),
)
1
  • You may find inspiration here Commented Nov 13, 2020 at 12:00

1 Answer 1

1

You need to use switchMap to map from outer observable to inner. And seeing that you have multiple HTTP calls from an array, you could use forkJoin to trigger them in parallel.

Try the following

outerObservable().pipe(
  switchMap((res): Collection => {
    forkJoin(res.tracks.items.map(track => this.trackService.fetchTrack(track.id))).pipe(
      map(tracks => ({
        id: res.id,
        name: res.name,
        description: res.description,
        tracks: tracks
      }))
    )
  })
).subscribe(...)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much it worked for me, but the one thing I had to do is to replace (res: Collection) by (res): Collection
Not sure why (res: Collection) wouldn't work but nevertheless I've updated the answer to reflect your change.

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.