I am creating a leaflet map with some line strings that I pull in as geoJSON via ajax. Works fine on areas with more line strings, but areas with less than 25 line strings it errors out claiming the object is null. Code is below. The geoJSON is valid I have tested it multiple times. I'm completely stuck.

function getFranchiseStreets(state_name, county, franchise) {

var data = "state_name=" + state_name + "&county=" + county + "&franchise=" + franchise;
    type: "POST",
    url: "php/franchiseStreets.php",
    data: data,
    dataType: 'html'
}).done(function (data) {

        var streets = $.parseJSON(data);

        var style = {
            "color": "blue",
            "weight": 5,
            "opacity": 1

if(streetsLayer != undefined || streetsLayer != null){
    map.eachLayer(function (layer) {
        if (map.hasLayer(streetsLayer)) {

//if the geometry object is empty then dont run this
if (isEmpty(streets.features)) {

    console.log('No Street Geometry empty'); 

} else {

    var streetsLayer = new L.GeoJSON(streets, {style: style}).addTo(map);


    .fail(function(xhr, status, error) {


  • Try putting async: false option in ajax. – TomazicM Feb 4 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.