Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subscribe method returns json object as undefined

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 : enter image description here

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.

like image 799
user3566272 Avatar asked Jul 29 '26 13:07

user3566272


1 Answers

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! :)
           });
like image 198
sebaferreras Avatar answered Aug 01 '26 05:08

sebaferreras