I am trying to use the following service in my code:
import {Injectable} from '@angular/core';
import {Http,Response} from "@angular/http";
import 'rxjs/Rx';
@Injectable()
export class HttpService{
constructor(private http : Http){}
getData(){
return this.http.get("URI")
.map( (res: Response) => res.json() );
}
}
The problem is, in run time it complains with:
res.json is not a function
I have defined the datatype of res as Response, but still complains
.map( (res: Response) => res.json() )
if i replace the map with subscribe it works fine:
.subscribe( res =>{
res.json();
console.log("City is:"+ res.json().city.name)
});
For what its worth, in Angular 5 I found that the res.json is not a function
error can be fixed by just returning res
if the response is pure json.
Try to import 'rxjs/add/operator/map';
instead of import 'rxjs/Rx';
. That should fix the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With