[GIS] ESRI Javascript API QueryTask error handling

arcgis-javascript-apijavascriptquery-task

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!

Best Answer

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.

Related Question