0

I'm trying to use query tasks to extract attributes of a feature based on what was selected from a filtering select list(setVal = "Chain Lakes Provincial Recreation Area"). But I get the error

TypeError: Cannot read property 'length' of undefined(...)

from the line of code

for (var i = 0, il = resultFeatures.length; i < il; i++) {

in the following:

 queryTask = new QueryTask("http://www.arcgis.com/sharing/rest/content/items/1d890f37f9f049d1b499141511235b5b/data");
 //initialize query
 query = new Query();
 query.returnGeometry = true;
 query.outFields = ["OC_NAME", "TYPE", "ACRES"];
 //create symbol for selected features
 symbol = new SimpleMarkerSymbol();
 symbol.setStyle(SimpleMarkerSymbol.STYLE_SQUARE);
 symbol.setSize(10);
 symbol.setColor(new Color([255, 255, 0, 0.5]));
 function showResults(featureSet) {
 //remove all graphics on the maps graphics layer
 map.graphics.clear();
 //Performance enhancer - assign featureSet array to a single variable.
 var resultFeatures = featureSet.features;
 //Loop through each feature returned
 for (var i = 0, il = resultFeatures.length; i < il; i++) {
 //Get the current feature from the featureSet.
 //Feature is a graphic
 var graphic = resultFeatures[i];
 graphic.setSymbol(symbol);
 //Set the infoTemplate.
 //graphic.setInfoTemplate(infoTemplate);
 //Add graphic to the map graphics layer.
 map.graphics.add(graphic);
 }
 };
 function executeQueryTask() {
 //set query based on what user typed in for population;
 query.where = "OC_NAME = '" + selVal + "'";
 console.log(query);
 //execute query
 queryTask.execute(query, showResults);
 };
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Oct 13, 2016 at 21:59
1
  • 1
    the error is telling you that the actual query didnt return any results. you should snoop the network traffic to inspect the actual request parameters and response to try and determine why. Commented Oct 14, 2016 at 20:40

1 Answer 1

1

you used url is not supported query task. check this url and look for source.

https://www.arcgis.com/home/item.html?id=1d890f37f9f049d1b499141511235b5b#overview

i have a suggestion but this suggestion run client side

esriRequest({
 url: "http://www.arcgis.com/sharing/rest/content/items/1d890f37f9f049d1b499141511235b5b/data",
 }).then(function (response) {
 features = response.layers[0].featureSet.features;
 featureLayer = new FeatureLayer(response.layers[0], {
 //infoTemplate: ...
 });
 map.addLayer(featureLayer);
 showResults(executeQueryTask("OC_NAME", 'Chain Lakes Provincial Recreation Area'));
 });
 function executeQueryTask(attribute, value) {
 return features.filter(function (feature) {
 return feature.attributes[attribute] == value;
 })
 };
 function showResults(features) {
 var extent = null
 features.forEach(function (graphic) {
 extent = extent ? extent.union(graphic.geometry.getExtent()) : graphic.geometry.getExtent();
 })
 if(extent) map.setExtent(extent.expand(2));
 }
answered Oct 15, 2016 at 17:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.