vendredi 15 mars 2019

Angular 7.2.5 + Typescript 3.2.4: Cannot read property 'aaa' of undefined

I have the following lines of code:

if ((this.axes.x && this.axes.x.property === 'run_number') ||
        (this.axes.y && this.axes.y.property === 'run_number')) {
        this.buildLineGraph(this.axes.y.property === 'run_number');
    } else {
        this.buildScatterGraph();
    }

most of the time runs without problems but from time to time fails if any of the element in the chains:

this.axes.x.property

this.axes.y.property

giving the error:

Cannot read property 'aaa' of undefined

with aaa being the undefined element: axes, x, y, etc.

How do I check for every possible undefined element? Do I need a long chain of nested if statement?

Aucun commentaire:

Enregistrer un commentaire