check if field exist and then run query

1873
7
Jump to solution
04-11-2017 01:09 AM
SibghatUllah1
Occasional Contributor
I am running a query but some map services doesn't contain field name "FeederID".So my query task is not running.
I have tried to get field names  by using getFieldList() and store it in an array and then use if conditions  using 
jQuery. i have also tried to copy my code inside  if(fieldtest.name=="FeederID") but not worked.

what i am looking is if  query is true on query3.where = "FeederID='HITAM POW F2'".


jQuery code:
if ($.inArray('example', myArray) != -1){  // found it}

My code:

on(dom.byId("search1"), "click", function () {
var typesData = [];
var allPromises = [];
var j=0;
var y=1;
for(j=15;j<19;j++){
var url="http://localhost:6080/arcgis/rest/services/RAEC/MapServer/"+j;
var queryTask3 = new QueryTask(url);
var query3 = new Query();
query3.returnGeometry = false;
getFieldList();
var options = [];
function getFieldList() {
esri.request({
url: "http://localhost:6080/arcgis/rest/services/RAEC/MapServer/"+j,
content: { f:"json" },
handleAs: "json",
callbackParamName: "callback",
load: function(response, io) {
var fields = response.fields;

dojo.forEach(fields, function(fieldtest) {
if(fieldtest.name=="FeederID")
{
options.push(fieldtest.name);
}
});
}
});
}
query3.where = "FeederID='HITAM POW F2'" ;
var promise = queryTask3.executeForCount(query3,function (count) {
//alert(count);
var typeCount={ field: y,
field1: count };
typesData.push(typeCount);
y++;
});

allPromises.push(promise);
options=[];

}
all(allPromises).then(function(){
var grid= new Grid({
columns: {
field: 'Serial', field1: 'Count' }
}, 'grid');
grid.renderArray(typesData);
});
});

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Your right there is a problem there. The if is not checking the Field.name property:

          promise.then(function(response){
            var fields = response.fields;
            array.some(fields, lang.hitch(this, function(fld){
              if(fld.name == "FeaderID"){
                options.push(response.id);
                return true;
              }
            }));
          });

View solution in original post

7 Replies
RobertScheitlin__GISP
MVP Emeritus

Sibjithat,

   Here is what I would do:

require([
    "esri/request",
    "dojo/_base/array",
    "dojo/promise/all",
    ....
], function(
    esriRequest,
    array,
    all,
    ....
){
      dom.byId("search1").disabled = true;
      options = [];
      function CheckFields() {
        var allPromises = [], promise;
        for (j = 15; j < 19; j++) {
          promise = esriRequest({
            url: "http://localhost:6080/arcgis/rest/services/RAEC/MapServer/" + j,
            content: { f: "json" },
            handleAs: "json",
            callbackParamName: "callback",
          });
          promise.then(function(response){
            var fields = response.fields;
            if(array.indexOf(fields, "FeederID") > -1){
              options.push(response.id);
            }
          });
          allPromises.push(promise);
        }
        return allPromises;
      }

      all(CheckFields()).then(function(results){
        //Now you know your app is ready and has checked all the layer s for the FeederID field.
        dom.byId("search1").disabled = false;
      })

      on(dom.byId("search1"), "click", function() {
        var typesData = [];
        var allPromises = [];
        var y = 1;
        array.map(options, function(id){
          var url = "http://localhost:6080/arcgis/rest/services/RAEC/MapServer/" + id;
          var queryTask3 = new QueryTask(url);
          var query3 = new Query();
          query3.returnGeometry = false;
          query3.where = "FeederID='HITAM POW F2'";
          var promise = queryTask3.executeForCount(query3, function(count) {
            //alert(count); 
            var typeCount = {
              field: y,
              field1: count
            };
            typesData.push(typeCount);
            y++;
          });
          allPromises.push(promise);
        });

        all(allPromises).then(function() {
          var grid = new Grid({
            columns: {
              field: 'Serial',
              field1: 'Count'
            }
          }, 'grid');
          grid.renderArray(typesData);
        });
      });‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
SibghatUllah1
Occasional Contributor

Dear Robert, 

Thank you for your time.I have copied your code into my application ,on button search click it is showing only datagrid columns  and nothing in console log.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

I only provided code to fix your field searching issue. You need to debug your grid populating code.

0 Kudos
SibghatUllah1
Occasional Contributor

Dear Robert,

I have run your code and find the same if condition problem.i have just removed the if line and code runs fine.So the problem is in the if statement.Please guide me me with this if statement:

 //if(array.indexOf(fields, "FeederID") > -1){
 options.push(response.id);
 alert(options);
 // }
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Your right there is a problem there. The if is not checking the Field.name property:

          promise.then(function(response){
            var fields = response.fields;
            array.some(fields, lang.hitch(this, function(fld){
              if(fld.name == "FeaderID"){
                options.push(response.id);
                return true;
              }
            }));
          });
SibghatUllah1
Occasional Contributor

Dear Robert,

Thank you very much.Finally the if condition is running.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Great,  Don't forget to mark this question as answered.

0 Kudos