Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm struggling to find the proper way to handle errors in a QueryTask. Particularly if the user clicks outside the feature layer.

My current QueryTask looks at a field (bare_earth) populated with either a 1 or a 0. I thought if I used...

if (bare_earth === 0) {
    do X;
}
else if (bare_earth === 1) {
    do y;
}
else{
    error handling;
}

However, clicks outside the feature layer return the following error:

TypeError {stack: "TypeError: Cannot read property 'attributes' of un…   at c (http://js.arcgis.com/3.9/init.js:74:436)", message: "Cannot read property 'attributes' of undefined"}
"TypeError: Cannot read property 'attributes' of undefined

Makes sense, since there are no attributes for the undefined area. That being said, how do I go about handling this?

For frame of reference, each of my three potential outcomes (1, 0, no data) alter the value of a variable (result) that is used to populate a DIV. For example, I want clicks outside of the feature to populate the DIV with "...outside the area...".

Thanks!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

It looks to me like you are trying to use the features array returned by QueryTask when it is empty. I.e. If 'result' is the name of the variable you're using in your QueryTask call back, then result.features is the array of features it returns. You need to check the length of the array, e.g.:

if (result.features && result.features.length > 0) {
    // do stuff with the features
} else {
    // do stuff when no features were found
}

If you don't think this is the issue, post the QueryTask success callback code you are using so we can have a closer look.

share|improve this answer
1  
This is exactly what I was looking for. Thank you! –  cmartin616 Jun 30 at 17:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.