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 error in the console is pointing to MapView.js of the API and not to any line in my script. Any suggestions on what is going wrong here?
Update1: 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 the setting of one of the properties of the layer -- which I need to set when the layer is created.
Update2: 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)
Update3: I am now almost sure that the issue is related to my featurelayer properties, as when I try map.remove with a hosted featurelayer, it works fine. The featurelayers are generated as follows:
function createLayer(graphics,renderer,type,fields,title){
lyr = new FeatureLayer({
source: graphics, //an array of esri/Graphic
fields:fields,
objectIdField: "ObjectID",
renderer:renderer,
spatialReference: {wkid: 4326},
geometryType: type,
title:title,
id:title
});
map.add(lyr);
}