I'm working with javascript modules to load spatial layers on a leaflet map. Everytime I try to add or style my GEOJSON layer called as a promise throught the function "getLayer" I created in my module, I get the error "layer.addTo" is not a function. Javascript basically doesn't recognize my GEOJSON layer as such. What am I missing ? Thanks.
The module that exports the layer :
// A mapping of paths to layers. Not exported, so only visible within this module
const layers = {};
// exported. this is the only part of the module visible from the outside.
export async function getLayer(path) {
if (!layers[path]) {
// if we don't already have it do the fetch and add it to the mapping
layers[path] = await fetchLayer(path);
}
// return the layer for the given path from the mapping
return layers[path];
}
// "private" functions for fetching and processing data
async function fetchLayer(path) {
return fetch('commerces_final', { credentials: 'include' })
.then(response => response.json())
.then(data => process1(data))
}
function process1(donnees){
return L.geoJson(donnees,
{
weight:12,
opacity: 0.1,
color: "#a19e83",
dashArray: '1',
fillOpacity: 1
}
);
}
The app module where I call the layer and try to add it :
import { getLayer } from '/js/toulouse.js';
async function app () {
var map = L.map('map', { drawControl: true });
const buildings = getLayer('commerces_final');
buildings.addTo(map)
}
app()