Subscribe method returns json object as undefined
I had a very interesting error today in my Ionic 2 project. I have an Asp.net REST API. I am calling from provider to rest api to get machines. The problem is it displays as JSON and when I print it in JSON format it shows an array of Json objects. However, after matching, when I subscribe, it shows undefined.
return this.http.get('http://localhost:19496/api/user(1982)/Machines',config).map(res=>console.log(res.json()));
this is normal. But when I subscribe it returns undefined data:
subscribe(data=>{
console.log(data)
})
I haven't changed the code. It worked for the last week, but when I got back from holiday and it didn't work.
source to share
Based on what I see in this question, the problem is that you are not returning anything from map
:
return this.http.get('...',config)
.map(res=>console.log(res.json()));
This is why it undefined
returns when you sign up for this. Try to return the response after printing it to the console:
return this.http.get('...',config)
.map(res => res.json())
.map(res => {
console.log(res);
return res; // <- Here! :)
});
source to share