Exception after remove and add a layer to web map / javascript

1792
5
12-20-2016 09:58 PM
PitersonPaulgek
New Contributor III

Hi,

Simple question: What is the correct way to remove a layer (ArcGISDynamicMapServiceLayer)?

We do constantly add and remove layers to/from a map object. We noticed that after remove and add there is an exception

Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
at Object._onLoadHandler (https://js.arcgis.com/3.17/init.js:1857:295)
at HTMLImageElement.<anonymous> (https://js.arcgis.com/3.17/init.js:63:154)

What is wrong? What is the meaning?

Tags (2)
0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Emeritus

Piterson,

   It is just map.removeLayer(layer); then layer = null; if you want to completely destroy that layer class.

0 Kudos
PitersonPaulgek
New Contributor III

Robert,

Thank you for your response,

We do not wont to destroy the layer - we will add it next time.

I understand that possible way is to make a layer visible/invisible, but what is wrong to remove/add layer?

Maybe the variable used with map.removeLayer(globalVariable)?

To be specific, it works like:

1)we create new layer

layerGlobalVariable1 = new ArcGISDynamicMapServiceLayer(......)

2)we put to array

dictionaryLayers[layerName1] = layerGlobalVariable1

3)we add to map

globalVariableCurrentLayer = dictionaryLayers[layerName1]

app.map.addLayer(globalVariableCurrentLayer )

4) app.map.removeLayer(globalVariableCurrentLayer)

5) app.map.addLayer(globalVariableCurrentLayer)

the exception is here, BUT not exactly on the row map.addLayer();

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Piterson,

   I have never had a need to add and remove a layer back and forth like that so I am not sure what is going wrong. I would always remove and recreate the layer personally.

0 Kudos
PitersonPaulgek
New Contributor III

Thanks, Robert,

I see.

I noticed that we create the array as Object:

dictionaryLayers= {};

And after that put layers:

dictionaryLayers[layerName1] = layerGlobalVariable1;

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Piterson,

  There does not seem to be anything wrong with what you are doing there. But I would re consider your workflow. Why is it necessary to try and hold on to a layer variable after it is removed from the map? This seems to me like a poor use of memory when you can destroy and reconstruct the layer as needed.