question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uncaught TypeError: Cannot read property 'enable' of undefined. (Polygon added after map load)

See original GitHub issue

I am loading a single pologon into the featureGroup after the map and feature group has been loaded, by doing an ajax call to a web service. Creating the polygon is successful, but when then attempting to edit the polygon through the “Edit Layers” control, a JS error is received: “Uncaught TypeError: Cannot read property ‘enable’ of undefined”. Objects are declared before data is received:

var myPolygon;
var myFeatureGroup = L.featureGroup();
var map = L.map('mapdiv').setView([-21.113887987117593, 149.14807319641113], 15);
L.tileLayer('http://{s}.tiles.mapbox.com/v3/##myaccount##/{z}/{x}/{y}.png', {
    maxZoom: 18
}).addTo(map);

Then, when data is received from the AJAX function for the polygon, it is added to the featureGroup as follows:

function getPolygonDetails_CallBack(response) {
    handleResponse(response.d.response);

    myPolygon = omnivore.wkt.parse(response.d.polygonGeography);
    myFeatureGroup.addLayer(myPolygon);
    myFeatureGroup.addTo(map);

    var drawControl = new L.Control.Draw({
        edit: {
            featureGroup: myFeatureGroup
        },
        draw: {
            polygon:true,
            polyline: false,
            rectangle: false,
            circle: false,
            marker: false
        }
    }).addTo(map);

    map.on('draw:created', function (e) {
        myFeatureGroup.addLayer(e.layer);
    });
}

Now - when the draw modify control is used, the error is displayed - as though it can no longer reference “myPolygon”.

image

Same result tested in leaflet 0.7.3 & 0.8-dev and leaflet.draw 0.2.2 & 0.2.3.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
ddproxycommented, Mar 9, 2016

@tyeware @dvector

Updated: https://jsfiddle.net/f9kmrhb7/86/

omnivore.wkt.parse() returns a GeoJSON object, which by default, is a FeatureGroup

Since FeatureGroup extends LayerGroup You can walk through the layers presented and add them individually to the FeatureGroup used for Leaflet.draw

0reactions
tyewarecommented, Mar 9, 2016

That’s awesome - thanks for the assistance.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Leaflet Draw "Cannot read property 'enable' of undefined ...
I am trying to use leaflet's edit function on polygons that I loaded from my database. When I click on leaflet's ...
Read more >
“Uncaught TypeError: Cannot read property 'x' of undefined ...
Hi, I'm getting the below error <b><i>most of the times</i></b> on every attempt to drag the map, after displaying a dynamically constructed polygon....
Read more >
Error Messages | Maps JavaScript API - Google Developers
This page describes the error messages that can be returned by the Maps JavaScript API. The Maps JavaScript API writes error and warning...
Read more >
Uncaught TypeError: Cannot read property of null - iDiallo
You are accessing a property of an object that is null. For example, document.getElementById('stuff') returns null. So adding .value will cause ...
Read more >
React Google Maps Api Style Guide
LoadScript - Loads the Google Maps API script; GoogleMap - The map component inside which all other components render. The simplest way to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found