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!
1 Answer 1
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.
-
1This is exactly what I was looking for. Thank you!cmartin616– cmartin6162014年06月30日 17:28:08 +00:00Commented Jun 30, 2014 at 17:28
Explore related questions
See similar questions with these tags.