Multiple GET requests with Angular 4/5

Asked

Viewed 581 times

1

I’m making a query in an API (SWAPI). The return is an object with all the information of the Star Wars characters. So far so good. But inside this object, I have some sublevels that are arrays and that besides some information, also has another URL that I need to consult to be able to print on the screen. The problem is that I’m not sure how to do this other query. To simplify, follow the return image:

inserir a descrição da imagem aqui

That’s the get of the service:

    getData(page?: string): Observable<any[]> {
    if(page){
      this.URL = page;
    }
    return this.http.get(this.URL)
    .map((response: Response) => <any[]>response.json())
    .catch(this.handleError);

  } 

I know there is a way to do with flatMap and forkJoin. I even tried, but I did not get the expected result

I followed that link: rxjs-observable

But I still couldn’t.

  • give a look at this link http://blog.danieleghidoli.it/2016/10/22/http-rxjs-observables-angular/

2 answers

0

Give a look, my solution would be to use a function that would help you and for a list of urls returns the observables of your requests. Then for each element that has this array of urls I call the function. Then you only have to map in order that you called. I am using the pipe operator depending on the version of rxjs you are using it is not available there and just do . mergeMap and so on.

Keep in mind that with forkJoin if a request fails all fail.

import { mergeMap } from 'rxjs/operators/mergeMap';
import { map} from 'rxjs/operators/map';
import { of } from 'rxjs/observable/of';
import { forkJoin } from 'rxjs/observable/forkJoin';

getItems(urls: string[]): Observable<any> {
    return <Observable<Post>> forkJoin(
        urls.map(url=> <Observable<Post>> this.httpClient.get(url))
     ).pipe(concatAll());
}

getData(page?: string): Observable<any[]> {
        if(page){
          this.URL = page;
        }
        return this.http.get(this.URL).pipe(
        mergeMap((person: any) => {
         return forkJoin(
            of(person),
            this.getItems(person.films)
            this.getItems(person.species)
      ),
       map((data: any[]) => {
          let person= data[0];
          let films= data[1];
          let species= data[2];
          person.films= films;
          book.species= species;
          return person;
        })
    }))        
} 
  • So it’s giving me some weird mistakes here, like, Cannot find name 'from'. in the first Return. Argument of type '{}' is not Assignable to Parameter of type 'string' no this.httpClient.get(url), Cannot find name 'forkJoin', Cannot find name 'on'. Strange is that they are all imported

  • What is your version of rxjs in package.json? Upgrade to a higher than 5.5.

  • So I upgraded to 5.5.10. The map I solved, I had put a comma instead of a dot. In the "of error, wouldn’t it be Observable.of(person)? I put Observable.of and the error is gone, but I don’t know if it would impact, since the error of "from" and URL remain :(

  • Observable.of also means that this is an old version, however all operators I used exist in previous versions tbm. Just see how you use them. Try Observable.of, Observable.forkJoin. the other only take the pipe and do with .

  • So, all the bugs I managed to solve, the problem is even in that "Return from" that says you can’t find the name and the url

  • I managed to solve "from", now the problem is no mergeMap kkkkkk Property 'mergeMap' does not exist on type 'Observable<string>'.

  • can use the of inves from

  • can use flatmap which is the same thing as mergemap

  • So friend, I switched Observable<Item> to any, and in this.httpClient.get(url) give me this error: "Argument of type 'string[]' is not Assignable to Parameter of type 'string'." This happens even if I put my model, from the same error E in mergeMap((person: any) give me this error: Argument of type '(person: any) => Unaryfunction<Observable<any[]>, Observable<any>>' is not Assignable to Parameter of type... I don’t know what else to do

  • I edited Get Items there, try it now.

Show 5 more comments

0

I see no problem using 2 GET methods, for example:

And I think I should create two interfaces one for each return ... so I could get the easy result:

interface Retono1{

  retorno: number;
  retorno1: string;
  retorno2: string;
  retorno3: string[];
  retorno4: number;

}

retorno:Retorno1;

 this.http.get(this.url)
      .subscribe(result => {
          this.retorno = result.json();
          //primeiro retorno
           this.http.get(this.url)
           .subscribe(result => {

                 //segundo retorno

            })

 });

And I would use logic to take the second payback for an interface as well...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.