Cannot read property of undefined 'BA' - a JSON response

I am getting this error and don’t know why:

this is my code: `

<button ion-button color="dark" round full (click)="goToHomePage()">Home Page</button>

<h2>{{NYSEstockData.BA.quote.change}}</h2>

`

In the nyse.html file and in nyse.ts my code is: `import { Component } from ‘@angular/core’;
import { IonicPage, NavController, NavParams } from ‘ionic-angular’;
import { HomePage } from ‘…/home/home’;
import { RestProvider } from ‘…/…/providers/rest/rest’;

/**

@IonicPage()
@Component({
selector: ‘page-nyse’,
templateUrl: ‘nyse.html’,
})
export class NysePage {

NYSEstockData: any;

constructor(public navCtrl: NavController, public navParams: NavParams, public restProvider: RestProvider) {
this.getNYSEDataObjectsFromPromise();
}

getNYSEDataObjectsFromPromise(){
this.restProvider.getNYSEDataFromAPIViaPromise().then(data => {
console.log(“Trying to access NYSE results from the Promise return”);

  this.NYSEstockData = data;

  console.log("Got NYSE results from the Promise");
  console.log(this.NYSEstockData);
})

}

ionViewDidLoad() {
console.log(‘ionViewDidLoad NysePage’);
}

public goToHomePage(){

this.navCtrl.push(HomePage); 

}

}
`

The thing is when I remove the HTML attempt to render the JSON data I can see the data in the console with no issues. I was able to get something similar working in another project in the home.ts and home.html pages accessing the data in the same way. Any help would be greatly appreciated!

Don’t abuse any like this. Take the time to write a business-level interface that describes what the data looks like. It will help your development environment protect you against silly typo bugs, will make your code self-documenting to help the next schmoe who has to try to maintain it (which is often future you).

Any controller property that is accessed by a template should be initialized, most preferably at the point of definition or in the constructor if you can’t do it at definition. This becomes absolutely critical for non-scalar properties. Arrays must be initialized to at least [] and objects to at least {}. Otherwise you get this error. Note that lifecycle events (such as ionViewWillEnter) are not viable places to do this.

1 Like