0

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 ) {

}
0

2 Answers 2

4

A cleaner way can be :

var current = Parse.User.current();

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

}
Sign up to request clarification or add additional context in comments.

Comments

0

Then check like this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.