문제

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

}
도움이 되었습니까?

해결책

A cleaner way can be :

var current = Parse.User.current();

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

}

다른 팁

Then check like this

if(Parse.User.current() !== null)
{
if( post.get("parent").id !== Parse.User.current().id ) {
//Do whatever necessary
}
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top