Today I had a very interesting bug in my Ionic 2 project. I have Asp.net REST API. I make a call from provider to rest api to get the machines. The problem is it is mapped as JSON and when I print its JSON format it shows the Json object array. However, after mapping when I subscribe it, it shows undefined.
return this.http.get('http://localhost:19496/api/user(1982)/Machines',config).map(res=>console.log(res.json()));
it shows as :

it is ok.But when I subscribe it, it returns the data undefined:
subscribe(data=>{
console.log(data)
})
I have not changed the code.Last week it was working, but when I returned from the holiday and it did not work.
Based on what I can see in the question, the problem is that you're not returning anything from the map:
return this.http.get('...',config)
.map(res=>console.log(res.json()));
That's why undefinedis being returned when you subscribe to that. Try by returning the response after printing it in the console:
return this.http.get('...',config)
.map(res => res.json())
.map(res => {
console.log(res);
return res; // <- Here! :)
});
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