Ionic 2, Cannot read property 'x' of undefined in a template, how to?


I am getting some data with a promise and trying to display it in a template, but i get Cannot read property 'x' of undefined.

Here is my code. this.someData.attributes will be undefined until the promise resolves, but how do i tell the view to display the data whenever that’s resolved?

export class SomePage {
    local: Storage = new Storage(LocalStorage);
    someData: Object;

    constructor(private app: App, private nav: NavController) {}

    ionViewDidEnter() {

        this.local.get('someData').then(resp => {
            this.someData= JSON.parse(resp);
            console.log(this.someData); // this works

    ngAfterViewInit() {

in the view:

<h4 *ngIf="someData.attributes !== undefined">

any ideas?

looks like the correct syntax is

<h4 *ngIf="someData">

the correct one would be

<h4 *ngIf="someData?.attributes">

i think.

the “?”-mark is like marking something as optional and only if this part is there evaluate the rest.

1 Like