Query problem

1023
5
04-04-2013 12:33 PM
SamirGambhir
Occasional Contributor III
Hi all,
I am running a queryTask and queryFeatures on a State and a sub-State feature layers respectively. The function is supposed to select features from the sub-State featureLayer which are above (or below) the State average (the input from the State feature Layer). I am being able to run the query on the sub-State layer, but I am not able to retrieve the State value. I'll appreciate it if someone can look into it. Here is the relevant code:
function queryParam(){
    var stateAvg;
    myURL = "subStateURL";
    queryTask = new esri.tasks.QueryTask(myURL);
    query = new esri.tasks.Query();
    query.returnGeometry = true;
    query.outFields = ["*"];
    query.where = "1=1";

    distFeatureLayer.setDefinitionExpression(someString);
    distFeatureLayer.queryFeatures(query, function(featureSet) {
        stateAvg=returnStateAverage(stateGeography, stateIndicator);
        for ( i = 0; i < featureSet.features.length; i++) {
            var indValue = featureSet.features.attributes[indicator];//'indicator' same as 'stateIndicator'
            if (userSelectedCriteria == "Above") {//user selects the criteria using a combobox
  if (indValue > stateAvg) {
                   selDist.push(featureSet.features.attributes.Dist_name);
      selStAbbr.push(featureSet.features.attributes.St_abbr);
  }
     } else if (userSelectedCriteria == "Below") {
  if (indValue < stateAvg) {
     selDist.push(featureSet.features.attributes.Dist_name);
     selStAbbr.push(featureSet.features.attributes.St_abbr);
  }
     }
        }
        strDist = "(Dist_name = '" + selDist[0] + "' AND St_abbr = '" + selStAbbr[0] + "')";
 if (selDist.length > 1) {
     for ( i = 1; i < selDist.length; i++) {
  strDist += "OR (Dist_name = '" + selDist + "' AND St_abbr = '" + selStAbbr + "')";
     }
 }
 distFeatureLayer.setDefinitionExpression(strDist);
        distFeatureLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW);
 distFeatureLayer.setRenderer(renderer);
 distFeatureLayer.refresh();
 hide("loaderExecute");
    });
}

function returnStateAverage(geography, indicator){
    var stateVal;
    var myURL = "stateURL";
    queryTask = new esri.tasks.QueryTask(myURL);
    query = new esri.tasks.Query();
    query.returnGeometry = false;
    query.outFields = ["*"];
    query.where = "State_name='" + geography + "'";
    queryTask.execute(query, function(fSet) {
        var features = fSet.features;
 dojo.forEach(features, function(feature) {
     stateVal = feature.attributes[indicator];
     return stateVal;
 });
    });
}
0 Kudos
5 Replies
StephenLead
Regular Contributor III
I haven't looked into your entire code sample - in fact I stopped on the 3rd line:

myURL = "subStateURL";
queryTask = new esri.tasks.QueryTask(myURL);


myURL doesn't look like a valid URL for the query task - it should be an ArcGIS Server REST endpoint. Is subStateURL meant to be a variable (not a string)?

If that doesn't help, can you step through your code in Firebug and let us know at which point it's failing?

Steve
0 Kudos
SamirGambhir
Occasional Contributor III
I haven't looked into your entire code sample - in fact I stopped on the 3rd line:

myURL = "subStateURL";
queryTask = new esri.tasks.QueryTask(myURL);


myURL doesn't look like a valid URL for the query task - it should be an ArcGIS Server REST endpoint. Is subStateURL meant to be a variable (not a string)?

If that doesn't help, can you step through your code in Firebug and let us know at which point it's failing?

Steve


Hi Steve,
Thanks for looking into it. Actually, I didn't want to put the whole URL as I wanted to keep it flexible for others to try it out with their data and services. I should have taken off the quotes. subStateURL and stateURLare replaced with the actually service url in my code.
The point where this code is failing is where I try to get the value for stateAvg using the function "returnStateAverage".
stateAvg=returnStateAverage(stateGeography, stateIndicator);

I tested the output in the function which returns the state value. Variable stateVal is correct when I write it to the console but it is not returning the value so I can assign it to var stateAvg.
Thanks
Samir
0 Kudos
OttarViken_Valvåg
New Contributor III
That's because your returnStateAverage function doesn't actually return any value. You only have a return statement in your anonymous function passed to the forEach() method.

Furthermore, all the logic following your returnStateAverage call needs to be moved to the result handler of your queryTask.execute call in the returnStateAverage function, since the queryTask.execute method works asynchronously (more formally, it returnes a dojo.Deferred object). The cleanest implementation would probably be to let your returnStateAverage function return a Deferred object.
0 Kudos
SamirGambhir
Occasional Contributor III
That's because your returnStateAverage function doesn't actually return any value. You only have a return statement in your anonymous function passed to the forEach() method.

Furthermore, all the logic following your returnStateAverage call needs to be moved to the result handler of your queryTask.execute call in the returnStateAverage function, since the queryTask.execute method works asynchronously (more formally, it returnes a dojo.Deferred object). The cleanest implementation would probably be to let your returnStateAverage function return a Deferred object.


Thanks for your comment,
You are right. Each time I tested the returned values from my function, it informed me that it is an object rather than a value. I'll try using the result handler to see if I can return the value. However, I am not clear what you mean by your last statement..."The cleanest...."
Samir
0 Kudos
OttarViken_Valvåg
New Contributor III
In case it wasn't clear, the reason you returnStateAverage function doesn't return anything is that the return statement marked in red doesn't return from that function - it returns from the function marked in blue.

function returnStateAverage(geography, indicator){
    var stateVal;
    var myURL = "stateURL";
    queryTask = new esri.tasks.QueryTask(myURL);
    query = new esri.tasks.Query();
    query.returnGeometry = false;
    query.outFields = ["*"];
    query.where = "State_name='" + geography + "'";
    queryTask.execute(query, function(fSet) {
        var features = fSet.features;
 dojo.forEach(features, function(feature) {
 stateVal = feature.attributes[indicator];
 return stateVal;
 });
    });
}


When I say "cleanest" I mean that it's the recommended practice when dealing with asynchronous methods. Recommended reading:
http://dojotoolkit.org/documentation/tutorials/1.6/deferreds/
0 Kudos