0

I'm trying to get the geometry in an array from a queryTask using the Esri JavaScript API v4.7. I'm sure I'm missing something but I dont know what. I want to take geometry from this to then create a new record in another service.

require([
"esri/tasks/QueryTask", 
"esri/tasks/support/Query"
], 
function(
QueryTask, 
Query
){
  var LayerUrl = "https://services8.arcgis.com/R1PAHHLiCM5plgJt/arcgis/rest/services/MICC_Planning/FeatureServer/0";
  var queryTask = new QueryTask({
    url: LayerUrl
  });
  var lotp="15MPH7941";
  var q="Lotplan LIKE '"+lotp+"'";
  var query = new Query();
  query.returnGeometry = true;
  query.outFields = ["*"];
  query.where = q;
  queryTask.execute(query).then(function(results){
    console.log(4);  
    window.alert(results.features.geometry);
  });

  // When resolved, returns a count of the features that satisfy the query.
  queryTask.executeForCount(query).then(function(results){
    console.log(results);
  });

});

1 Answer 1

1

Your code is ok, the problem is that the QueryTask returns a result object, that contains a FeatureSet (which is an array of features). Then if you would like to get the first feature, you have to use results.features[0]

So instead of window.alert(results.features.geometry); you have to get the geometry from every result feature:

results.features.forEach(function (feature)
    window.alert(feature.geometry);
});

I have created a jsbin that you can check if you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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