Skip to content
Advertisement

Angular Http Subscribe not working

Hi In my Angular Component, i have this code in one of my methods

this.http.get("http://localhost:8080/poeples")
.map(
    resp => { resp = resp.json(); }
).subscribe(
    (data) => { this.poeples = data; },
    err =>  console.log(err)
);

In network tab in chrome dev inspector i saw that my get call returning result, but data is undefined.

Why?

Advertisement

Answer

The reason it was not working originally, is because you had this:

resp => { resp = resp.json(); }

You are not returning a value. When you use the curly braces, you have to explicitly define a return value. All you had to do was:

resp => { return resp.json(); }

Or remove the braces:

resp => resp.json() 
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement