Eduscho
Eduscho

Reputation: 459

Javascript IF statement with null condition error

In the following IF statement, one of the conditions is sometimes null.

Parse.User.current() can be null, in which case I'll get this error:

Uncaught TypeError: Cannot read property 'id' of null

Is there an elegant way to avoid this error?

if( post.get("parent").id != Parse.User.current().id ) {

}

Upvotes: 0

Views: 218

Answers (2)

Rahul
Rahul

Reputation: 77846

Then check like this

if(Parse.User.current() !== null)
{
if( post.get("parent").id !== Parse.User.current().id ) {
//Do whatever necessary
}
}

Upvotes: 0

A cleaner way can be :

var current = Parse.User.current();

if(current && post.get("parent").id !== current.id ) {

}

Upvotes: 4

Related Questions