AnsweredAssumed Answered

map.remove throws type error after removing the layer in ArcGIS JS API 4.3

Question asked by darbariNCSU on May 7, 2017
Latest reply on May 8, 2017 by TSolow-esristaff

I am trying to remove a layer from the mapView.

var LayertoRemove=map.findLayerById(layerID);
map.remove(LayertoRemove);

The layer is found and is actually removed, but after it's removed it continues to throw TypeError: this.controller.destroy is not a function, and the rest of the script is not run. when I remove these two lines of code, the script works fine. the full error log is:

 Uncaught TypeError: this.controller.destroy is not a function   MapView.js:510
   
at Object.d.detach (MapView.js:510)
   
at Object.update (MapView.js:25)
   
at k._animationFrame (init.js:329)

The error in the console is pointing to MapView.js and init.js of the API and not to any line in my script. 

NOTE: The FeatureLayers that I am trying to remove are not hosted layers, but generated from client-side geometries (graphics are created from two csv files taken from the client); I suspect that the error is happening because of the setting of one of the properties of the layer -- which I probably need to set when the layer is created.  map.remove() works fine when I tested it with with a hosted layer. The FeatureLayers are generated as follows and are added to map without any issue:

function createLayer(graphics,renderer,type,fields,title){
   
lyr = new FeatureLayer({
      
source: graphics,
      
fields:fields,
      
objectIdField: "ObjectID",
      
renderer:renderer,
      
spatialReference: {wkid: 4326},
      
geometryType: type,
      
title:title,
      
id:title
      
});
   
map.add(lyr);    }

Outcomes