Hey,
I have a loop that runs through all items and sends the address of the item to a html, in the html I use google geocoding to get the lat/lng and send it data back to the pagecode.
my code:
for (let item of res.items) {
let address = item.address.formatted.replaceAll(" ", "+");
$w("#html1").postMessage({ address: address });
await new Promise((resolve) => {
$w("#html1").onMessage(async (event) => {
console.log("received message");
item.lat = event.data.lat;
item.lng = event.data.lng;
await wixData.update(database, item);
resolve();
});
});
}
But the console.log(“received message”) stacks with the loop, that means: in the first loop I get one console.log(), in the second loop I get two console.log(), …
I know this problem from javascript, if you use .addEventListener(“message”) multiple times, but it shouldn’t appear in a .onMessage.
Is there any solution in Velo to remove the event, or do you have an idea, how I can change the code?
Because if I’d use this code, I always get undefined back:
async function runLoop() { //get the items
for (let item of res.items) {
let address = item.address.formatted.replaceAll(" ", "+");
$w("#html1").postMessage({ address: address });
await new Promise((resolve) => {
let msg = HTMLonMessage();
console.log(msg); //update database
resolve();
});
}
}
let HTMLonMessage = $w("#html1").onMessage((event) => {
let lat = event.data.lat;
let lng = event.data.lng;
return [lat, lng];
});