Angular - res.json() is not a function
Had a similar problem where we wanted to update from deprecated Http module to HttpClient in Angular 7. But the application is large and need to change res.json() in a lot of places. So I did this to have the new module with back support.
return this.http.get(this.BASE_URL + url)
.toPromise()
.then(data=>{
let res = {'results': JSON.stringify(data),
'json': ()=>{return data;}
};
return res;
})
.catch(error => {
return Promise.reject(error);
});
Adding a dummy "json" named function from the central place so that all other services can still execute successfully before updating them to accommodate a new way of response handling i.e. without "json" function.
Don't need to use this method:
.map((res: Response) => res.json() );
Just use this simple method instead of the previous method. hopefully you'll get your result:
.map(res => res );
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?
You can remove the entire line below:
.map((res: Response) => res.json());
No need to use the map method at all.