cnak2
cnak2

Reputation: 1841

Unable to check for non-existent field in object in angular 4

I have an angular 4 application where I'm trying to insert a parameter if it exists. But I keep getting the message:

ERROR TypeError: Cannot read property 'item' of null

The object I'm working with essentially is:

contact: {
    shared: {
        item: string,
        _id: string
    }
}

Here is how I'm checking to see if the 'item' value exists:

this.contact.shared.linkedin.item !== null ? this.share.linkedin = this.contact.shared.linkedin : null;

The problem is when it returns without the 'item', like this:

contact: {
    shared: {
        _id: YYUIDIODU989S9FSO8F0O8SDF09
    }
}

I then get the error:

ERROR TypeError: Cannot read property 'item' of null

I've tried numerous ways to check for this, but nothing works. I've checked to see if the value is null, undefined, etc.

What am I doing wrong here???

Thanks!

Upvotes: 0

Views: 76

Answers (1)

zmag
zmag

Reputation: 8251

Error occurred because of linkedin, not item.

check if this.contact.shared.linkedin is null.

Upvotes: 1

Related Questions