Hi All I have written the below code to get the feature count. I have declared m_featCount as global variable, and assigning value to it in getStateCount(featureSet) function and calling this function in twnqueryTask.execute(). In getStateCount() funciton, m_featCount value showing perfectly. When I try to access the variable m_featCount in another function MyLogic() or GetState(evt), I am getting null value. I can write my logic in getStateCount() function, but I need the value of m_featCount variable throughout the application. Please help me how resolve this issue.
var m_featCount; function GetState(evt) { var twnqueryTask = new esri.tasks.QueryTask("http://myserver/WSIPL/rest/services/myservice/MapServer/3"); var twnquery = new esri.tasks.Query(); twnquery.returnGeometry = true; twnquery.outFields = ["LNDKEY", "SECTIONKEY"]; twnquery.geometry = evt.mapPoint; twnqueryTask.execute(twnquery, getStateCount); } function getStateCount(featureSet) { m_featCount= featureSet.features.length; }
Function MyLogic() { if(m_featCount>0){ // I will write my logic here but in this function Iam getting featCount = 'undefined' } else { // I will write my logic here } }
I'd suggest adding an error return function to the QueryTask.execute call to do two things: 1) identify any errors on the server side and 2) to set your variable value to 0 so you don't get "undefined" value errors later in your code.