AnsweredAssumed Answered

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

Question asked by zapkingdude on Aug 16, 2016
Latest reply on Aug 17, 2016 by zapkingdude

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

Attachments

Outcomes