Ionic 2 Promise returns objects stringified


I’ve a local array variable and I fetch(based on some condition) an element(which is an object with some properties). While returning the object all the property names are stringed, hence can’t be used to display it on the template. json() or JSON.parse() throws error :frowning:

constructor(public http: Http) { = [ {name: 'Eyes', code: 1}, {name: 'Ears', code: 2}, {name: 'Nose', code: 3}, {name: 'Mouth', code: 4}, {name: 'Hands', code: 5} ]; } loadId(id){ for(var i=0; i<(; i++) { if([i].code == id) { return Promise.resolve([i]); } } };

Inside component:
constructor(public navCtrl: NavController, public navP: NavParams, public data: Data) {'code')).then(result => { = result; }); }

But when I try to retrieve value using property name: {{}}
I get the error: name of undefined

Why is it so? I’m getting stringified object back from the promise. When I used json() or JSON.parsed around my returned result, it throws more errors.


I think your problem lies elsewhere, because this code works for me. Of course, you have to seed company with a dummy value in the component constructor, or else the template will try to access it before the promise resolves.


What you said is right, when I initiate the variable with some dummy value it works.

Doesn’t it defeat the purpose of using a Promise?
I’m using similar logic in some other page and its working fine with out initializing it in the constructor. So am confused …


I don’t think so. You can throw up a loading spinner until the promise resolves. It’s just sort of the cost of doing business in a reactive world.


may be instead of
try it on


@rapropos, Very well said, “It’s just sort of the cost of doing business in a reactive world.”. Thanks mate for helping me out.

@Thavarajan, That would surly work. Thanks a lot for helping …Will keep this ngOnInit thing in my mind. I never knew about it. Thank you once again.