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
constructor(public http: Http) { this.data = [ {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<(this.data).length; i++) { if(this.data[i].code == id) { return Promise.resolve(this.data[i]); } } };
Inside component:
constructor(public navCtrl: NavController, public navP: NavParams, public data: Data) { this.data.loadId(this.navP.get('code')).then(result => { this.company = result; }); }
But when I try to retrieve value using property name: {{company.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.