Target must be an event emitter

4003
11
04-15-2014 10:23 AM
MattTenold
New Contributor III
I am currently using the Dojo/on and I keep receiving the Error: Target must be an event emitter
http://js.arcgis.com/3.9/
Line 212

error in this function:

function runQuery() {
   var queryTask = new esri.tasks.QueryTask("http://gis.phila.gov/ArcGIS/rest/services/PhilaGov/Police_Incidents_Last30/MapServer/0");
   
   var query = new esri.tasks.Query();
   query.returnGeometry = true;
   query.where = '1=1';
   query.outFields = ["DC_DIST", "POINT_X", "POINT_Y"];
   
   map.on(queryTask, "onComplete", function(featureSet) {

    var cL = new clusterlayerNew({
     displayOnPan: false,
     map: map,
     features: featureSet.features,
     infoWindow: {
      template: new esri.InfoTemplate("${SECTOR}"),
      width: 325,
      height: 100
     },
     flareLimit: 15,
     flareDistanceFromCenter: 20
       });

             map.addLayer(cL);

   });
  };
0 Kudos
11 Replies
DasaPaddock
Esri Regular Contributor
Try: queryTask.on("complete", function(event) {});

See:
https://developers.arcgis.com/javascript/jsapi/querytask-amd.html#event-complete
0 Kudos
MattTenold
New Contributor III
I can't do
queryTask.on("complete", function(event) {}); 
since I am constructing a clusterlayer using the function features from my AMD class "clusterlayerNew."   Is there a way to get the features from the function "features" while running after the  queryTask is complete?
0 Kudos
ReneRubalcava
Frequent Contributor
How about

on(queryTask, 'complete', function(){});


You lost me there, but the execute method returns a Promise, so you can also do.
queryTask.execute(query).then(someFunction);
0 Kudos
DasaPaddock
Esri Regular Contributor
How about this?
function runQuery() {
  var queryTask = new esri.tasks.QueryTask("http://gis.phila.gov/ArcGIS/rest/services/PhilaGov/Police_Incidents_Last30/MapServer/0");

  var query = new esri.tasks.Query();
  query.returnGeometry = true;
  query.where = '1=1';
  query.outFields = ["DC_DIST", "POINT_X", "POINT_Y"];

  queryTask.on("complete", function(event) {

    var cL = new clusterlayerNew({
      displayOnPan: false,
      map: map,
      features: event.featureSet.features,
      infoWindow: {
        template: new esri.InfoTemplate("${SECTOR}"),
        width: 325,
        height: 100
      },
      flareLimit: 15,
      flareDistanceFromCenter: 20
    });

    map.addLayer(cL);

  });
};
0 Kudos
MattTenold
New Contributor III
I tried both methods.  But this won't work either.  The main problem is all of my events are handled in my Dojo class.  I attached the main html page and the dojo class with the logic that the main page accesses.

function runQuery() {
   var queryTask = new esri.tasks.QueryTask("http://gis.phila.gov/ArcGIS/rest/services/PhilaGov/Police_Incidents_Last30/MapServer/0");
   
   var query = new esri.tasks.Query();
   query.returnGeometry = true;
   query.where = '1=1';
   query.outFields = ["DC_DIST", "POINT_X", "POINT_Y"];
   
   queryTask.on("complete", function(event) {

    var cL = new clusterlayerNew({
     displayOnPan: false,
     map: map,
     features: event.featureSet.features,
     infoWindow: {
      template: new esri.InfoTemplate("${SECTOR}"),
      width: 325,
      height: 100
     },
     flareLimit: 15,
     flareDistanceFromCenter: 20
       });

             map.addLayer(cL);

   });
  };
0 Kudos
DasaPaddock
Esri Regular Contributor
I just noticed that `runQuery` is missing a call to `queryTask.execute(query)`. Can you try adding this before the queryTask.on line?
0 Kudos
MattTenold
New Contributor III
Tried that also.  Didn't work.  It keeps saying Target must be an event emitter.
0 Kudos
DasaPaddock
Esri Regular Contributor
Is this code still in the html page?
  on(queryTask, "onError", function(err) {
  alert(err.details);
  });

  queryTask.execute(query);


Can you remove it since outside the runQuery function, queryTask is the esri/tasks/QueryTask module rather than an instance of a QueryTask.
0 Kudos
MattTenold
New Contributor III
Tried that.  It runs but it keeps jumping around.  It doesn't go through the functions.  It is really weird.  I set a break point but it won't step through the functions.  I am still looking for why it doesn't continue running through the functions.
0 Kudos