4

I am trying to display the query results of a button click on a map.

I am getting the following error at this line: queryTask.execute(cntryq); of my script:

TypeError {stack: "TypeError: undefined is not a function↵ at coun…ndler (http://js.arcgis.com/3.10/init.js:490:212)", message: "undefined is not a function"}

message: "undefined is not a function" stack: "TypeError: undefined is not a function↵ at countries (http://tobagoborn.com/esri/examples/p1p2c.html:149:15)↵ at cntrycds (http://tobagoborn.com/esri/examples/p1p2c.html:163:5)↵ at http://tobagoborn.com/esri/examples/p1p2c.html:179:7↵ at S._resolve (http://js.arcgis.com/3.10/js/esri/layers/FeatureLayer.js:64:294)↵ at k (http://js.arcgis.com/3.10/js/esri/layers/FeatureLayer.js:65:355)↵ at c (http://js.arcgis.com/3.10/init.js:74:221)↵ at d (http://js.arcgis.com/3.10/init.js:74:10)↵ at resolve.callback (http://js.arcgis.com/3.10/init.js:75:350)↵ at Object.n._resDfd (http://js.arcgis.com/3.10/init.js:758:31)↵ at e._successHandler (http://js.arcgis.com/3.10/init.js:490:212)"


MY SCRIPT

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Display Feature layer Query results</title>
<link rel="stylesheet" href="http://js.arcgis.com/3.10/js/esri/css/esri.css">
<script src="http://js.arcgis.com/3.10/"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
</head>


<body class="claro">
<div class="btn-group btn-group-lg">
<button type="button" class="btn btn-default" id="NSM" value="NSM">News</button>
<button type="button" class="btn btn-default" id="SDG" value="SDG">SDG Status</button>
<button type="button" class="btn btn-default" id="CMP" value ="CMP">City Models/Plans</button>
<button type="button" class="btn btn-default" id="CSM" value ="CSM">Story Maps</button>
<button type="button" class="btn btn-default" id="EDU" value ="EDU">Educational Games</button>
<button type="button" class="btn btn-default" id="USR" value ="USR">User Submitted</button>
<button type="button" class="btn btn-default" id="OPM" value ="OPM">Project Map</button>
</div>

<div name="container" id="container">
<div id="mapDiv"></div>
<div id="sidecol"> </div>
</div>

<script>
  var countries;
  var buttonPicked;
  var buttonType;
  var countryCode = [];
  var whrString = ""
  var map, featureLayer, relatedTable;

var featureURL = "http://dev.consciousglobalchange.org/arcgis/rest/services/MEP/webmap_catalog/FeatureServer/0";
var relatedURL = "http://dev.consciousglobalchange.org/arcgis/rest/services/MEP/webmap_catalog/FeatureServer/1";

    require(["esri/map", "dojo/domReady!"], function(Map) 
    { 
        map = new Map("mapDiv", 
        {
            center: [-5.049, 38.485],
            zoom: 2,
            basemap: "gray"
        });
    });

function clickType(e) {
  if (!relatedTable) 
  {
    // The related table hasn't loaded properly yet. Abort!
    alert("Still waiting to connect to the related table.");
    return;
  }

  // "this" refers to the button that was clicked, since this is function added to onClick.
  // "value" reads the "value" attribute from the button.

  var type = this.value;
  require([ //"esri/tasks/query"
  "esri/map", 
  "esri/tasks/query", 
  "esri/layers/FeatureLayer",
  "esri/tasks/QueryTask",
  "esri/tasks/RelationshipQuery",
  "dojo/domReady!"
  ], function(FeatureLayer,Query,QueryTask,on, RelationshipQuery)
  {
    var q = new Query();  //RESULTS OF BUTTON CLICK QUERRY PASSED TO relatedTable.queryFeatures fuction 
        q.outFields = ["*"]; 
        q.where = "TYPE = '" + type + "'";
        q.outFields = ["un_country"];

        function countries(uncode) //DISPLAYS RESULTS OF QUERY FOR COUNTRIES
        {
            //build query task
            alert( 'country codes: ' + uncode);
            var queryTask = new QueryTask(featureURL);
            //build query filter
            var cntryq = new Query();
            cntryq.returnGeometry = true;
            cntryq.outFields = ["*"];
            cntryq.where =  "UN in (" + uncode + ")";

            //Can listen for onComplete event to process results or 
            //can use the callback option in the queryTask.execute method.
            dojo.connect(queryTask, "onComplete", function(featureSet) 
            {
                map.graphics.clear();
                var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, 
                new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, 
                new dojo.Color([255,255,255,0.35]), 1),new dojo.Color([125,125,125,0.35]));

                //QueryTask returns a featureSet.  Loop through features 
                //in the featureSet and add them to the map.
                var data = featureSet.features; 
                console.log(data);

                dojo.forEach(data,function(feature){
                    var graphic = feature;
                    graphic.setSymbol(symbol);
                    map.graphics.add(graphic);
                });
            });
            **queryTask.execute(cntryq);**
        } //end of Countries


        function cntrycds(allcds)  //CREATES A STRING PASSED TO countries function
        {                          
            //alert(allcds);
            whrString = ""
            for (var i = 0; i < allcds.length; i++) 
            {
                //alert(allcds[i]);
                whrString += allcds[i] + ", ";
            }
            whrString = whrString.slice(0, -2);  
            countries(whrString);
            //alert(allcds);
        }

            relatedTable.queryFeatures(q, function(featureSet) 
            {
                countryCode = [];
                //console.log(featureSet.features);
                var data = featureSet.features; 
                for (i = 0; i < data.length; i++) 
                {
                    countryCode[i] = data[i].attributes.un_country;
                    //console.log(data[i].attributes.un_country);
                    //alert(countryCode[i]);
                }

                cntrycds(countryCode);
            });
  });  //end of require 
} //end clickType

require([
  "esri/map", 
  "esri/layers/ArcGISTiledMapServiceLayer", 
  "esri/layers/FeatureLayer",
  "esri/tasks/QueryTask",
  "esri/tasks/RelationshipQuery",
  "dojo/domReady!"
  ], function(Map, ArcGISTiledMapServiceLayer, FeatureLayer,Query,QueryTask,on, RelationshipQuery)  
  { 
        var table = new FeatureLayer(relatedURL, 
        {
            mode: FeatureLayer.MODE_ONDEMAND,
            outFields: ["*"]
        });

        table.on("load", function (e) 
        {
            relatedTable = e.layer;
        });

    }); //end require



$('#NSM').click(clickType); 
$('#SDG').click(clickType); 
$('#CMP').click(clickType); 
$('#CSM').click(clickType);
$('#EDU').click(clickType);
$('#USR').click(clickType);
$('#OPM').click(clickType);
</script>
 </body>

 </html>
  • I am a newbie when it comes to esri's js-api. I am actually combining 2 scripts. The first part (uses a Query) I figured out how to return the country codes from a table when you click a button. The 2nd part (uses a QueryTask) I figured out how to display the countries based on an array of county codes. Now I need to combine them. I plan to review the code once I get it working. – ChrisJ Oct 20 '14 at 16:31
4

Make sure that the order of dependencies in your require statement match the order of the corresponding variable names. For example, the correct order for the second require should be require([ "esri/map", "esri/tasks/query", "esri/layers/FeatureLayer", "esri/tasks/QueryTask", "dojo/on", "esri/tasks/RelationshipQuery", "dojo/domReady!" ], function(Map, Query, FeatureLayer, QueryTask, on, RelationshipQuery)

  • Thanks ashima, I reviewed my require method and made the necessary changes. I am still getting the same error – ChrisJ Oct 20 '14 at 16:51
  • Hmm. Did you also fix the third require? Because when I run it, I don't get any error. – ImmenselyHappy Oct 20 '14 at 17:15
  • I've created a JSFiddle for it - jsfiddle.net/798wbh4b – ImmenselyHappy Oct 20 '14 at 19:02
  • Thanks Ashima for the jsfiddle!! Found the cause of the error... The 2nd require dependencies were out of order... – ChrisJ Oct 21 '14 at 14:01
  • @ashima Despite having the correct order, I get "Uncaught Reference: Geometry Service is not defined" error! How to get the ordering between dojo/esri js right? – ujjwalesri Oct 31 '17 at 3:47
2

For starters this isn't valid Html:

**queryTask.execute(cntryq);**

I removed the ** symbols from line and the code ran fine. If you want to comment something out in code, use the // symbol in javascript.

Part 2:

Used the Chrome Developer console to find the problem. Some of your buttons at the top pass a null into your CountryCode method. The javascript then attempts to execute queryTask.execute(Null); which blows the queryTask up since there is nothing to possibly perform a query on. You'll need to check for null and not call that line if the county code is null.

  • Sorry for the confusion, I used the ** to try to bold the code so you can find it. I don't want to comment it out... That is the piece of code that is causing the error. – ChrisJ Oct 20 '14 at 16:04
  • Thanks for pointing out the null issue.. It's also blowing up for the the ones that are not Null. – ChrisJ Oct 20 '14 at 16:23
  • Because your recursive searching eventually ends in a null. – ChargerIIC Oct 20 '14 at 16:25
  • ChargerllC... Are you saying for example the first button returns a code of 332 as well as an null? If yes, how were able to "see" that it is returning a null also? – ChrisJ Oct 20 '14 at 16:35
  • Anytime you see 'undefined' in your error, that's letting you know that an null(also called undefined in javascript) occurred. Using Chrom dev console, I put a break point on the line throwing the error. If there is no related Country Code, your javascript is still executing, putting a null in since no country code was actually found. – ChargerIIC Oct 20 '14 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.