AnsweredAssumed Answered

Can't "save" a feature from QueryTask. Argh!

Question asked by evtguy on Oct 3, 2012
Latest reply on Oct 8, 2012 by evtguy
This is probably pretty easy but I can't seem to get what I'm doing wrong. I have a function, kicked off via a button click, that queries a particular layer, and then does some additional queries & data retrieval against three other layers. Basically, I have one location and I want to extract information from three other datasets that overlap its spatial extent.

Anyways, what I've tried to do is an initial query to my layer which has my spatial location (using an FID which I pass to the function). Once I query that layer, I was to "save" the feature to a variable so I can quickly use it in my subsequent queryTask calls. Here's my simple code:

function getProjectReport(theFid) {  var theParam = theFid.split(",");   var query = new esri.tasks.Query();      query.where = "FID=" + theParam[0];  query.outFields = ["*"];  query.returnGeometry = true;    var theGraphic;    //Specify the appropriate point/line map service depending on what was clicked  switch (theParam[1]) {   case "point":    var queryTask = new esri.tasks.QueryTask("URL #1");      break;   case "polyline":    var queryTask = new esri.tasks.QueryTask("URL #2");      break;  }          queryTask.execute(query,function(featureSet) {     dojo.forEach(featureSet.features, function(feature) {      theGraphic = feature;                 });  });     console.log(theGraphic.attributes.PROJECT); }

Now, the queryTask finds my record but when it gets to my "console.log" line, it errors out and tells me that my variable theGraphic is undefined. I've defined my variable at the beginning of my function so why doesn't my assignment persist after the end ofmy forEach loop? How do I fix this so I can re-use theGraphic later on in my function?

Thanks again!