0

I am looking to assign as an object a Fetch API promise from a local GeoJSON file. Here is the code

fetch("data/sites.geojson")
        .then(function(response) {
            return response.json();
        })
        .then(function(data) {
            L.geoJSON(data, {
                pointToLayer: styles_sites
            }).addTo(map);
        });
    };

I tried the call back method, as advised here Saving fetched JSON into variable

(EDIT) New code, but there is still a missing formal parameter

function getData("data/sites.geojson", cb) {
fetch("data/sites.geojson")
    .then(function(response) {
        return response.json();
    })
    .then(function(data) {
        L.geoJSON(data, {
            pointToLayer: styles_sites,
            onEachFeature: function (feature, layer) {
                layer.on('mouseover', function() { 
                    layer.openPopup(layer.bindPopup("<b>"+feature.properties.nombre+"</b>"))
                });
                layer.on('mouseout', function() { 
                    layer.closePopup(); 
                });
                layer.on('click', function () {
                    layer.bindPopup("<b>Nombre: </b>"+feature.properties.nombre+"<br><b>Barrio: </b>"+feature.properties.barrio+"<br><b>Tipo: </b>"+feature.properties.tipo+"<br><b>Ubicacion: </b>"+feature.properties.ubicacion+"<br><b>Correo: </b>"+feature.properties.contacto);
                });
            }
        }).addTo(map);
    .then(function(result) {
        cb(result);
        });
    });
};
getData("data/sites.geojson", function (data) {
    return console.log({data});
});
David Buck
  • 3,752
  • 35
  • 31
  • 35
LinoHub
  • 1
  • 1

2 Answers2

0

Most probably just incorrect syntax of your callback function:

// Use either arrow function
getData("data/sites.geojson", (data) => {
    return console.log({data});
});

// or standard function
getData("data/sites.geojson", function (data) {
    return console.log({data});
});
ghybs
  • 47,565
  • 6
  • 74
  • 99
0

I found the way to work this out by adding within the fetch function, what I originally wanted to do on the map.

This was to add a L.controlLayer using the geojson as overlay.

This is the code that made it work:

let sites = getData()
    .then((function(data) {
        L.geoJSON(data, {
            pointToLayer: styles_sites,
            onEachFeature: function LayerControl(feature, layer) {
                var popupText = "<b>" + feature.properties.nombre + "<br>";
                layer.bindPopup(popupText);
                category = feature.properties.tipo;
                 // Initialize the category array if not already set.
                if (typeof categories[category] === "undefined") {
                    categories[category] = L.layerGroup().addTo(map);
                    layersControl.addOverlay(categories[category], category);
                }   
                categories[category].addLayer(layer);
                layer.on('mouseover', function() { 
                    layer.openPopup(layer.bindPopup("<b>"+feature.properties.nombre+"</b>"))
                });
                layer.on('mouseout', function() { 
                    layer.closePopup(); 
                });
                layer.on('click', function () {
                    layer.bindPopup("<b>Nombre: </b>"+feature.properties.nombre+"<br><b>Barrio: </b>"+feature.properties.barrio+"<br><b>Tipo: </b>"+feature.properties.tipo+"<br><b>Ubicacion: </b>"+feature.properties.ubicacion+"<br><b>Correo: </b>"+feature.properties.contacto);
                });
            }
        }).addTo(map);
    }));

Actually it comes from one of your answer on another post ghybs.

David Buck
  • 3,752
  • 35
  • 31
  • 35
LinoHub
  • 1
  • 1