Search code examples
angularobjecttypeof

Angular4 - check if nested object item is empty


I'm checking if a nested object item named "token" is empty or not, in AngularIDE with Angular4

if (typeof this.user.data.token !== "undefined")

this is throwing <Cannot read property 'token' of null>

Should I necessarily check for every nested object existance?


Solution

  • You have to ...

    if (this.user && this.user.data && this.user.data.token) {
    }