"Error: Target must be an event emitter" gets thrown whenever I search multiple layers in the map

2574
4
Jump to solution
08-16-2016 05:18 AM
PrasannaSivakumar
New Contributor II

I have several layers getting loaded according to the user's rights towards them. Forexample. I have rights to access only to 5 layers out of 10, I will be viewing only 5 layers in my map and not all the layers. So, I dynamically generate it. Now, for search option to be present for every layer, The sources for the search was given from the ajax call. The layers are getting listed in the dropdown perfectly, but when I search in a particular layer or in "ALL" category it is throwing the following error:

init.js:199 Error: Target must be an event emitter(…) "Error: Target must be an event emitter

var searchParams = [];

  $.ajax({
                         type: "POST",
                         url: "WebService.asmx/featurelist",//getting the allotted feature lists for this particular user
                         data: "{ 'aData': '" + userid + "'}",
                         contentType: "application/json; charset=utf-8", 
                         dataType: "json", 
                         success: function (data) {
                             var myData = JSON.parse(data.d);
                            // console.log(myData);
                             var url1 = serviceLoc;
                             var url2 = "/MapServer/0";
                             for (var i = 0; i < myData.length; i++) {
                                 searchParams.push({
                                     featureLayer: ""+ finalurl + "",
                                     name: "" + myData[i].Fname + "",                                                          
                                 });
                             }
                         }
            });

var search = new Search({
                     map: map,                    
                     sources: [],
                     zoomScale: 5000000,
                     placeholder : " Object id / Facility No "
                 }, "shoo");

                 search.on("load", function () {

                     var sources = search.sources;
                    // console.log(JSON.stringify(searchParams));
                    
                    for (var i = 0; i < searchParams.length; i++) {
                        sources.push({
                             featureLayer: searchParams[i].featureLayer,
                             name: searchParams[i].name,
                             placeholder: "Object ID",
                             enableLabel: false,
                             searchFields: ["OBJECTID"],
                             displayField: "OBJECTID",
                             supportsPagination: true,
                             enableSuggestions: true,
                             enableSuggestionsMenu: true,
                             suggestionTemplate: "Object ID: ${OBJECTID}",
                             exactMatch: false,
                             maxResults: 10,
                             maxSuggestions: 10,
                             infoTemplate: new InfoTemplate("Object ID - ${OBJECTID}", "Ipsum Doler")
                     }//Set the sources above to the search widget
                     search.set("sources", sources);
                 });
                 search.startup();

If I hard code every layer on the sources array, it is working fine, but it is not working fine if I load it dynamically.  I am clueless now, I request any of you give some solutions for it.

Thanks in advance

0 Kudos
1 Solution

Accepted Solutions
thejuskambi
Occasional Contributor III

While populating the searchParams you are storing the featureLayer property as url, and the same is assigned to sources. The featureLayer has to be of type FeatureLayer. Try this.

searchParams.push({
    featureLayer: new FeatureLayer(finalurl),
    name: myData[i].Fname
});‍‍‍‍‍‍‍‍‍

View solution in original post

4 Replies
thejuskambi
Occasional Contributor III

While populating the searchParams you are storing the featureLayer property as url, and the same is assigned to sources. The featureLayer has to be of type FeatureLayer. Try this.

searchParams.push({
    featureLayer: new FeatureLayer(finalurl),
    name: myData[i].Fname
});‍‍‍‍‍‍‍‍‍
PrasannaSivakumar
New Contributor II

It worked like a charm, thanks a ton for the quick reply.

Btw, if you would explain me why sending it as a string doesn't work there, it will be great for my learning.

anyhow, Thank you,

0 Kudos
FC_Basson
MVP Regular Contributor

The featureLayer property of the source object requires a FeatureLayer object. Search | API Reference | ArcGIS API for JavaScript 3.17 

<FeatureLayer> featureLayerRequired

This applies only to map service feature layer sources. The feature layer is queried in the search. If searching feature layers, this is required.

PrasannaSivakumar
New Contributor II

I understood it now, thanks @FC Basson

0 Kudos