Leaflet.draw icon indicating copy to clipboard operation
Leaflet.draw copied to clipboard

clearLayers throws an error: Cannot read property 'length' of null

Open cazacugmihai opened this issue 6 years ago • 8 comments

When calling LayerGroup#clearLayers, an exception is thrown on this line: https://github.com/Leaflet/Leaflet.draw/blob/c6af99b761c5d9900bf5a3e2e1d8827bbde29542/src/edit/handler/Edit.SimpleShape.js#L66

Error:

TypeError: Cannot read property 'length' of null
    at NewClass.removeHooks (leaflet.draw-src.js:2379)
    at NewClass.eval (leaflet.draw-src.js:2795)
    at NewClass.fire (leaflet-src.js:593)
    at NewClass.removeLayer (leaflet-src.js:6573)
    at NewClass.removeLayer (leaflet-src.js:6715)
    at NewClass.removeLayer (leaflet-src.js:6861)
    at NewClass.eachLayer (leaflet-src.js:6774)
    at NewClass.clearLayers (leaflet-src.js:6735)

Can you check first for null, before iterating the _resizeMarkers, please?

Environment:

leaflet: 1.3.1
leaflet-draw: 1.0.2

Thanks!

cazacugmihai avatar Apr 02 '18 09:04 cazacugmihai

I have changed the source code and another error appears, mentioning that the this._map is null.

The fix consists in replacing this line with: if (shape._map && this._map).

cazacugmihai avatar Apr 02 '18 09:04 cazacugmihai

I can confirm that critical bug. It seems like removeHooks() is being called two times and the second time it just can't find the previously removed stuff anymore. CircleMarker and and Circle both register the same listener on remove. I guess that is the reason.

eisverticker avatar Apr 05 '18 15:04 eisverticker

Sorry for promotion but:

As leaflet.draw development seemed to came to a halt

I recommend to use new plugin leaflet.pm In our project it was best choice

TravnikovDev avatar Apr 06 '18 10:04 TravnikovDev

I created a second pull request regarding this issue, I think @cazacugmihai solution is more a workaround than a bugfix. RemoveHooks should be called only once (i guess). https://github.com/Leaflet/Leaflet.draw/pull/853

eisverticker avatar Apr 06 '18 11:04 eisverticker

@eisverticker: I've tested your fix and it works. I'll delete my PR. Thank you!

cazacugmihai avatar Apr 06 '18 21:04 cazacugmihai

I had same error during adding/removing shapes.

I've fixed the error by disabling editing mode before removing the layer.

layer.editing.disable(); that.drawGroup.removeLayer(layer);

aolszak avatar Jun 07 '18 09:06 aolszak

I have the same problem. Will have any solution for this? @aolszak your tip works, thanks!

emirdeliz avatar Aug 04 '18 23:08 emirdeliz

doesn't work still :( used the if (shape._map && this._map) solution

herrigor avatar Oct 25 '21 13:10 herrigor