Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - res.json() is not a function

Tags:

angular

I am having an issue with my API Service. This service connects to my nodejs backend api.

The error says

ERROR TypeError: res.json is not a function 

I am getting this error after recently updated this service to use the HTTPClient instead of Http. Im getting this reponse because im missing the old http with the new? if thats the case is there an new Response and how do i use it?

import { Injectable } from '@angular/core'; import { environment } from '../../environments/environment'; import { HttpHeaders, HttpClient, HttpParams } from '@angular/common/http'; import { Response } from '@angular/http'; import { Observable } from 'rxjs/Rx'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch';  import { JwtService } from './jwt.service';  @Injectable() export class ApiService {   constructor(     private http: HttpClient,     private jwtService: JwtService   ) {}    private setHeaders(): HttpHeaders {     const headersConfig = {       'Content-Type': 'application/json',       'Accept': 'application/json'     };     if (this.jwtService.getToken()) {       headersConfig['Authorization'] = this.jwtService.getToken();     }     return new HttpHeaders(headersConfig);   }    private formatErrors(error: any) {      return Observable.throw(error.json());   }    get(path: string, httpParams: HttpParams = new HttpParams()): Observable<any> {     return this.http.get(`${environment.api_url}${path}`, { headers: this.setHeaders(), params: httpParams })     .catch(this.formatErrors)     .map((res: Response) => res.json());   }    put(path: string, body: Object = {}): Observable<any> {     return this.http.put(       `${environment.api_url}${path}`,       JSON.stringify(body),       { headers: this.setHeaders() }     )     .catch(this.formatErrors)     .map((res: Response) => res.json());   }    post(path: string, body: Object = {}): Observable<any> {     return this.http.post(       `${environment.api_url}${path}`,       body,       { headers: this.setHeaders() }     )     .catch(this.formatErrors)     .map((res: Response) => res.json());   }    delete(path): Observable<any> {     return this.http.delete(       `${environment.api_url}${path}`,       { headers: this.setHeaders() }     )     .catch(this.formatErrors)     .map((res: Response) => res.json());   } } 
like image 420
Kay Avatar asked Oct 08 '17 12:10

Kay


People also ask

Why Res JSON is not a function?

The "response. json is not a function" error occurs when we call the json() method on an object that is not the Response object that resolves from the promise the fetch() method returns, or call the json() method on the return value from calling an axios method. To solve the "response.

What does res JSON () do?

json() Function. The res. json() function sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using the JSON.


2 Answers

HttpClient.get() applies res.json() automatically and returns Observable<HttpResponse<string>>. You no longer need to call this function yourself.

See Difference between HTTP and HTTPClient in angular 4?

like image 134
Max Koretskyi Avatar answered Sep 27 '22 20:09

Max Koretskyi


You can remove the entire line below:

 .map((res: Response) => res.json()); 

No need to use the map method at all.

like image 44
Debarati Majumder Avatar answered Sep 27 '22 21:09

Debarati Majumder