Select to view content in your preferred language

querytask returns no errors but undefined variables!

4364
12
Jump to solution
06-30-2015 04:42 PM
LefterisKoumis
Frequent Contributor

This function returns undefined variable. The first alert returns: something found:1

the second alert returns "Odometer 1: undefined.

I have a very similar code shown below under GOOD CODE that it returns the expected value.

Both code execute the same querytask.

What's wrong in the first block code? Thanks.

function queryTask_odometer1_executeCompleteHandler(results)

  {

  if (results.features && results.features.length > 0) {

  alert("something found: " + results.features.length);

  resultCount=results.features.length;

  for (var i = 0; i < resultCount; i++) {

  var featureAttributes = results.features.attributes;

  for (var attr in featureAttributes) {

  if (attr = "Odometer")

  { alert(attr + "1:  " + featureAttributes[attr]);

  }

  }

  }

  }else{

  alert("nothing found");

  }

  //alert(mydata);

  }

GOOD CODE

function showResults (results) {

  var resultItems = [];

  var resultCount = results.features.length;

  alert (resultCount);

  for (var i = 0; i < resultCount; i++) {

  var featureAttributes = results.features.attributes;

  for (var attr in featureAttributes) {

  if (attr = "Odometer")

  { alert(attr + "1:  " + featureAttributes[attr]);

  resultItems.push("<b>" + attr + ":</b>  " + featureAttributes[attr] + "<br>"); 

  }

  }

  }

       //   dom.byId("info").innerHTML = resultI

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

  OK I found the problem is was a CaSe issue.

You have outfields all lower case:

queryTask_odometerParams1.outfields=["Odometer"];

When the property is actually outFields with a capital F

queryTask_odometerParams1.outFields = ["Odometer"];

What this caused is no fields beside the display field to be returned by the query.

View solution in original post

12 Replies
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

  Your issue is that your are making an assignment to attr by using a single equal sign to evaluate a condition you must use a double or triple equal sign:

if (attr === "Odometer") {
  alert(attr + "1:  " + featureAttributes[attr]);
}
0 Kudos
OwenEarley
Frequent Contributor

This issue occurs in the Good Code block as well.

0 Kudos
LefterisKoumis
Frequent Contributor

Actually, no. I get a response with the correct value.alert.jpg

Thanks.

Here is the complete good code, modified from one of the samples in the esri's JS API website.

<!DOCTYPE html>

<html>

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <!--The viewport meta tag is used to improve the presentation and behavior of the samples

      on iOS devices-->

    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">

    <title>Query State Info without Map</title>

    <script src="http://js.arcgis.com/3.13/"></script>

    <script>

      require([

        "dojo/dom", "dojo/on",

        "esri/tasks/query", "esri/tasks/QueryTask", "dojo/domReady!"

      ], function (dom, on, Query, QueryTask) {

        var queryTask = new QueryTask("<place you url>);

        var query = new Query();

        query.returnGeometry = false;

        query.outFields = ["Odometer"];

        on(dom.byId("execute"), "click", execute);

        function execute () {

       

  query.where = <place your condition>  ;

          queryTask.execute(query, showResults);

        }

        function showResults (results) {

  var resultItems = [];

  var resultCount = results.features.length;

  alert (resultCount);

  for (var i = 0; i < resultCount; i++) {

  var featureAttributes = results.features.attributes;

  for (var attr in featureAttributes) {

  if (attr = "Odometer")

  { alert(attr + "1:  " + featureAttributes[attr]);

  resultItems.push("<b>" + attr + ":</b>  " + featureAttributes[attr] + "<br>"); 

  }

  resultItems.push("<br>");

  }

  }

       //   dom.byId("info").innerHTML = resultItems.join("");

        }

      });

  

  </script>

  </head>

  <body>

    US state name :

    <input type="text" id="stateName" value="474">

    <input id="execute" type="button" value="Get Details">

    <br />

    <br />

    <div id="info" style="padding:5px; margin:5px; background-color:#eee;">

    </div>

  </body>

</html>

0 Kudos
LefterisKoumis
Frequent Contributor

Thanks. I agree, however the issue persists even after the correction. The strange thing is that the Good code even though has the same incorrect evaluation (attr = "Odometer"), it does work by providing a response with the correct value.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

   Sure it would work as long as the query has a "Odometer" attribute in the results. Nothing is really jumping out at me as to why one would work and the other would not Assuming that both use the same exact Query where clause and same outfields. Can you provide more code for review?

0 Kudos
LefterisKoumis
Frequent Contributor

Sure. Here is the "bad code". I already posted the good code in another response. I ensure you that the "where" and  "url" lines are identical in both good and bad code since I copied and pasted. Thank you.

<!DOCTYPE html>

<html>

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <!--The viewport meta tag is used to improve the presentation and behavior of the samples

      on iOS devices-->

    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">

    <title>Shapes and Symbols</title>

    <link rel="stylesheet" href="http://js.arcgis.com/3.13/esri/css/esri.css">

    <style>

      #info {

        top: 20px;

        color: #444;

        height: auto;

        font-family: arial;

        right: 20px;

        margin: 5px;

        padding: 10px;

        position: absolute;

        width: 115px;

        z-index: 40;

        border: solid 2px #666;

        border-radius: 4px;

        background-color: #fff;

      }

      html, body, #mapDiv {

        padding:0;

        margin:0;

        height:100%;

      }

      button {

        display: block;

      }

    </style>

    <script src="http://js.arcgis.com/3.13/"></script>

    <script>

      var map;

      require([

  "esri/map",

  "esri/layers/DynamicMapServiceLayer",

  "esri/tasks/QueryTask",

  "esri/tasks/query",

  "esri/symbols/SimpleMarkerSymbol",

  "esri/tasks/FeatureSet",

  "esri/InfoTemplate",

  "dojo/_base/Color",

  "dojo/dom",

  "dojo/on",

  "dojo/domReady!"

      ], function(       

  Map, DynamicMapServiceLayer, QueryTask, Query, SimpleMarkerSymbol, FeatureSet, InfoTemplate, Color, dom, on

      ) {

        map = new Map("mapDiv", {

          basemap: "streets",

          center: [-25.312, 34.307],

          zoom: 3

        });

        map.on("load", readurl);

        function readurl() {

              var queryTask_odometer1 = new QueryTask("<place you url>);

  var queryTask_odometerParams1 = new Query();

  queryTask_odometerParams1.geometry = false;

  queryTask_odometerParams1.outfields=["Odometer"];

queryTask_odometerParams1.where= <place your condition>  ;

  //if (map.loaded) {

     queryTask_odometer1.execute(queryTask_odometerParams1,queryTask_odometer1_executeCompleteHandler,onError);

  //} else {

  // map.on("load", function () {

  // queryTask_odometer1.execute(queryTask_odometerParams1,queryTask_odometer1_executeCompleteHandler,onError);

  // });

  }

  function onError(error) {

  console.log(error);

  }

  function queryTask_odometer1_executeCompleteHandler(results)

  {

  if (results.features && results.features.length > 0) {

  alert("something found: " + results.features.length);

  resultCount=results.features.length;

  for (var i = 0; i < resultCount; i++) {

  var featureAttributes = results.features.attributes;

  for (var attr in featureAttributes) {

  if (attr === "Odometer")

  { alert(attr + "1:  " + featureAttributes[attr]); 

  }

  }

  }

  }else{

  alert("nothing found");

  }

  //alert(mydata);

  }

      

      });

  

    </script>

  </head>

 

  <body>

   

    <div id="mapDiv"></div>

  </body>

</html>

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

  I know you are trying to protect your data but can you show me what your condition looks like?

queryTask_odometerParams1.where = < place your condition > ;

0 Kudos
LefterisKoumis
Frequent Contributor

Here is:

_County_selectedItem= "ALA";

  _Route_selectedItem= 112;

  _PM1_selectedItem= 0.5;

  queryTask_odometerParams1.where= "County = '" + _County_selectedItem + "'" + " AND Route = " +  _Route_selectedItem+ " AND PMc = '" + _PM1_selectedItem + "'"  ;

However, I think I am getting close. The root of the problem is the loop

for (var attr in featureAttributes)

If I remove the condition,

for (var attr in featureAttributes) {

  //if (attr === "Odometer")

  //{

  alert(attr + "1:  " + featureAttributes[attr]); 

  // }

Then the alert shows only the first attribute which is not the Odometer. That explains, why I get no response.

Why the loop does not iterate through the rest of the attributes and stops at the first one. The bizarre point is that in the good code, when I used attr = "Odometer", it displays the value just fine!!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

  OK I found the problem is was a CaSe issue.

You have outfields all lower case:

queryTask_odometerParams1.outfields=["Odometer"];

When the property is actually outFields with a capital F

queryTask_odometerParams1.outFields = ["Odometer"];

What this caused is no fields beside the display field to be returned by the query.