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

1626
1
05-07-2017 03:04 PM
REZAAmindarbari2
New Contributor

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);    } 
0 Kudos
1 Reply
ThomasSolow
Occasional Contributor III

I don't see anything wrong with the code you posted.

Removing a client-side feature layer seems to work fine in this sample: ArcGIS API for JavaScript Sandbox (I just added some code to remove the layer on click, seems to work fine).

You could take a look at that sample closely and see if you're doing anything different.

0 Kudos