Select to view content in your preferred language

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

1258
10
Jump to solution
10-03-2012 12:10 PM
SteveCole
Honored Contributor
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!
Steve
0 Kudos
10 Replies
SteveCole
Honored Contributor
Many thanks to everyone's input in this thread. Getting this to work has been one of the toughest challenges for me since I started working with the javascript API. I do have things finally working but I did have to rethink my entire process. As several people pointed out, the callback routines were critical so I re-wrote my code to be less linear and more "modular." I had resisted this because of another constraint (this whole process will update an HTML template opened via an onClick event) but I did find a way. So to recap my solution...

My button's onClick event starts everything by calling this function:
function getProjectReport2(param) { 
 var theParam = param.split(",");
 var theShape;
 var query = new esri.tasks.Query();    
 query.where = "FID=" + theParam[0];
 query.outFields = ["*"];
 query.returnGeometry = true;

 //Specify the appropriate point/line map service depending on what was clicked
 switch (theParam[1]) {
 case "point":
   var queryTask = new esri.tasks.QueryTask("http://dmc-arcgis.snoco.co.snohomish.wa.us/SnocoGISdev/rest/services/transportation/tipProjectsPoint/MapServer/0");   
   break;
 case "polyline":
   var queryTask = new esri.tasks.QueryTask("URL to Map Servce");    
   break;
 }

 //The new browser window must be opened in the function called by the 'onClick' event so
 //the new window is created here and its associated variable is created with a global scope.
 //Calculate the center of the screen for placement of the new window
 var center_left = (screen.width / 2) - (1000 / 2);
 var center_top = (screen.height / 2) - (600 / 2); 
 newWindow = window.open('','mywindow','width=1000,height=600,menubar=yes,scrollbars=yes,left=' + center_left + ',top=' + center_top); 

 var dQuery = queryTask.execute(query,createDeferredResults);  
}


...which then passes the feature onto a separate callback function. I decided to use a deferredList as Kelly had suggested:
function createDeferredResults(featureSet) {
 var dTractRace, dTractIncome, dTractLep;

 dojo.forEach(featureSet.features, function(feature) {
   theTipProject = feature;       
 });
    
 var query = new esri.tasks.Query();    
 query.outFields = ["*"];
 query.returnGeometry = true;
 query.geometry = theTipProject.geometry;
 
 qTractRace = new esri.tasks.QueryTask("URL Service #1");
 qTractIncome = new esri.tasks.QueryTask("URL Service #2");
 qTractLep = new esri.tasks.QueryTask("URL Service #3");
 
 dTractRace = qTractRace.execute(query);
 dTractIncome = qTractIncome.execute(query);
 dTractLep = qTractLep.execute(query);
 
 defQuery = new dojo.DeferredList([dTractRace,dTractIncome,dTractLep]);
 defQuery.then(handleCensusResults);
}


Finally, another callback function where I finally get down to the task of updating my HTML template:
function handleCensusResults(results) {
 var raceResults, incomeResults, lepResults;

 raceResults = results[0][1].features;
 incomeResults = results[1][1].features;
 lepResults = results[2][1].features;
 .
        .
        .
 processRaceResults(raceResults);
}


Once again, thank you ALL for helping me!

Cheers,
Steve
0 Kudos