0

I am displaying some results from Parse on a website but currently the loop is breaking because not all the columns have values in them.

var warningCreator = object.get("creator").get("country");

There are some instances where the user has not selected their country and that results in this error.

I am wondering if there is any way to check whether the result is undefined before calling it.

For iOs I found:

if ([object objectForKey:@"keyPath"]) {
  // the object has a value for key keyPath
}

Is there anything simialar for the javaScript SDK?

*Edit (Added more code for clarity):

        var WarningPoints = Parse.Object.extend("Warning_Point");
        var query = new Parse.Query("Warning_Point");

        query.include("creator");
        query.include("position");

        query.find({
            success: function(results) {
                markerCount = 0;

                if (results.length == 0) {
                    alert('There are Warning Points found for specified parameters.')
                };

                for (var i = 0; i < results.length; i++) {

                    var object = results[i];

                    var warningCreator = object.get("creator").get("country");

                    var warningLat = object.get("position").get("lat");
                    var warningLong = object.get("position").get("long");

                    if((object.get("position").get("address"))!=null){
                        warningAddress = object.get("position").get("address");
                    }

                    console.log(warningAddress);

                    var warningName = object.get("name");

                }
            },
            error: function(error) {
                alert("Error: " + error.code + " " + error.message);
            }
        });

The exact error in console is:

Uncaught TypeError: Cannot read property 'get' of undefined - and the line of the error is: warningCreator = object.get("creator").get("country");

4
  • just try to check the null or empty value or UNDEFINED... Commented Sep 8, 2015 at 8:55
  • The probelem is when doing the check, the error is triggered, see below: if((object.get("creator").get("country"))!=null){ warningCreator = object.get("creator").get("country"); } Commented Sep 8, 2015 at 9:01
  • 1
    i think you need to do 2 checks. 1 for creator and other for checking country. The two values can be null/empty. Commented Sep 8, 2015 at 9:12
  • If you add the above as an answer, I can mark it correct as that is 100% correct. Commented Sep 8, 2015 at 10:11

2 Answers 2

1
if(object['keyPath']!=null){
 //the object has a value for key keyPath
}

Where object is the variable that contains your data retrieved from parse. You can be more accurate in your condition checking for empty values too.

In your case:

if(object.get("creator")!=null && object.get("creator").get("country")!=null){
 //the fields have a value
}
else{
 //Some field is null
}

You need to do a check for each .get("field") in your statement because each field can be null.

You have 2 nested .get().get() and probably the first one is returning null so it raises the exception.

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

2 Comments

How would I use this for for a relational object, like object.get("creator").get("country");
Can you put more code? how do you obtain the "object" var? it would help me in order to give you a better answer
1

thanks for the help.

Sometimes you just need the opinion of some fresh eyes to freshen your views again.

if(object.get("creator") != null ){
                    if(object.get("creator").get("country") != null ){
                        var warningCreator = object.get("creator").get("country");
                    }
                }

                if(object.get("position") != null ){
                    if(object.get("position").get("lat") != null ){
                           var warningLat = object.get("position").get("lat");
                    }
                }

                if(object.get("position") != null ){
                    if(object.get("position").get("long") != null ){
                          var warningLong = object.get("position").get("long");
                    }
                }

                if(object.get("position") != null ){
                    if(object.get("position").get("address") != null ){
                         var warningAddress = object.get("position").get("address");
                    }
                }

1 Comment

i do the same in one if statement and i add the explanation of the nested checks. It is enough for your vote?

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.