5

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)
            });
2
  • 1
    Are you sure your request is returning json? Commented Aug 26, 2016 at 8:45
  • I had this issue when I had functionA() calling functionB() and both used a call to map(). By the time functionA map() was called B's map had already transformed the res.json() into an array of objects which meant that A had no json available. Fix was to remove the map() call from functionB(), so only the top level map() was called Commented Dec 23, 2016 at 17:09

5 Answers 5

18

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.

Sign up to request clarification or add additional context in comments.

Comments

6

Try to import 'rxjs/add/operator/map'; instead of import 'rxjs/Rx';. That should fix the problem.

3 Comments

Unless the result is not correct JSON, I believe Stefan is right.
Although this code may help to solve the problem, it doesn't explain why and/or how it answers the question. Providing this additional context would significantly improve its long-term educational value. Please edit your answer to add explanation, including what limitations and assumptions apply.
@TobySpeight If I had an explanation, don't you think I would provide it? I had the same problem a few days ago and this is how I fixed it. I can't think of any reason why import 'rxjs/Rx' wouldn't work because reference to rxjs/add/operator/map exists in Rx.js file, so I guess it is some sort of bug, but I'm not 100% sure, so I didn't mention it at all, just wanted to provide fix for the problem.
1

You Don't need to use this: .map((res: Response) => res.json() ); or .map(res => res.json()); because HttpClient.get() feeds or applies to res.json() automatically and returns Observable<HttpResponse<string>>.

You no longer need to call this function yourself again. Just simply put it this way: .map(res => res ); this resolves or solve the problem.

Comments

0

You do not need to call res.json() if you response is already a Json you can directly work with res you do not even need to use map in this case

  getData(){
  return  this.http.get("URI");
}

Comments

-2

Try to put only like this

.map((response: Response) => response)

This work surely in Angular

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.