user3566272
user3566272

Reputation: 55

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.

Upvotes: 3

Views: 1045

Answers (1)

sebaferreras
sebaferreras

Reputation: 44659

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! :)
           });

Upvotes: 4

Related Questions