Search code examples
angularrxjs

Nested request Rxjs Angular


I have a list of items. Each item contains the name of a file. I need the name of each file. With each name make a new request and get the data file First request looks like->first item

The goal is get the list and get the name of the image and do a new request for each item-image to get the path of the image and finaly append the path to the first item list The goal looks like your textThe goal->

The code gets my try

entradas$.pipe(
mergeMap(entradas=>forkJoin 
 (entradas.map(ent=> 
 this.entradaService.getStaticSingularImage(ent.imagen))))) 
 .subscribe(data=>console.log(data))

Solution

  • You can change your code to combine the response and the details together.

    entradas$
    .pipe(
        mergeMap(entradas=>{
           return forkJoin(entradas.map(entrada => {
               return this.entradaService
                          .getStaticSingularImage(entrada.imagen)
                          .pipe(map((response) => {
                             return { 
                                entrada: entrada,
                                response
                             }
                          }));
           });
        })
    )
    .subscribe(data=>console.log(data))