Skip to content

Commit

Permalink
fix async geocode function (#577)
Browse files Browse the repository at this point in the history
Inside the nested async function of `geocode()` you have no access to `this`. So I removed the nested function and defined `geocode` as async itself. Then it works.
  • Loading branch information
rteitge authored Sep 9, 2024
1 parent 1f8f25a commit 3d6dc84
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions Resources/Public/JavaScript/esm/leaflet-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,31 +200,28 @@ class LeafletBackendModule {
.classList.remove("active");
}

geocode() {
async function fetchGeoData() {
try {
let response = await fetch(this.geoCodeUrl);
let data = await response.json();

if (data.length === 0) {
response = await fetch(this.geoCodeUrlShort);
data = await response.json();
}
async geocode() {
try {
let response = await fetch(this.geoCodeUrl);
let data = await response.json();

if (data.length === 0) {
response = await fetch(this.geoCodeUrlShort);
data = await response.json();
}

if (data.length !== 0) {
const firstResult = data[0];
if ("lat" in firstResult) {
this.latitude = firstResult.lat;
}
if ("lon" in firstResult) {
this.longitude = firstResult.lon;
}
if (data.length !== 0) {
const firstResult = data[0];
if ("lat" in firstResult) {
this.latitude = firstResult.lat;
}
if ("lon" in firstResult) {
this.longitude = firstResult.lon;
}
} catch (error) {
console.error("Error fetching geo data:", error);
}
} catch (error) {
console.error("Error fetching geo data:", error);
}
fetchGeoData();
}
}

Expand Down

0 comments on commit 3d6dc84

Please sign in to comment.