4

How does this code change wrt to the new HttpClientModule, where mapping from response.json() is not required.

    //uses Http
    getLegalTerms(): Observable<legalterm[]> {
        return this._http.get(this._legalTermUrl)
        .map((response: Response) => <legalterm[]> response.json());
    }

i get nothing back if I do the following

    //uses HttpClientModule
    getLegalTerms(): Observable<legalterm[]> {
        return this._http.get<legalterm[]>(this._legalTermUrl)
    }

I am subscribing in the component class in the same way in either case

     ngOnInit() {
        this._legalTermService.getLegalTerms()
         .subscribe((legalTerms: LegalTerm[])  =>  {
         this.legalTerms = legalTerms;
      })
    }

I get data in the grid with Http but no data in grid with HttpClient

Thank you! Anand

6
  • The code looks correct. What do you mean you get nothing back? The angular http observable does not run unless you subscribe to it. Commented Nov 7, 2017 at 16:04
  • @LLai I am subscribing to this method in the component class Commented Nov 7, 2017 at 16:33
  • so the http request is firing and you do receive legalTerms? Commented Nov 7, 2017 at 16:39
  • @LLai - yes, answered below Commented Nov 7, 2017 at 17:05
  • 1
    Yes, it does work. not sure why this would not work before... Commented Nov 8, 2017 at 16:49

1 Answer 1

2

It should be,

 getLegalTerms(): Observable<legalterm[]> {
        return this._http.get(this._legalTermUrl)
        //.map((response: Response) => <legalterm[]> response);
       .map((response: Response) => <any> response);
 }
Sign up to request clarification or add additional context in comments.

4 Comments

I gave the upvote as it was a correct answer. but somebody gave the downvote for no reason (might be the question owner I guess). these people don't have the courage to appreciate those who help
@Sajeetharan - thanks but the problem is type Reponse cannot be converted to LegalTerm[]
try changing as any and then convert inside the component
This IS correct for the OLD Http not the NEW HttpClient. If you are using the new HttpClient you should NOT need to map. The OP's syntax was correct and this is NOT.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.