Await in loop

Hi.
update() is asynchronous so try to await it.
https://www.wix.com/velo/reference/wix-data/update

change your function to async and add “await” before wixData.update().
Then the function will be waiting for resolving the Promise, that resolves when update is successful.

export async function button_click() {
    // your code
    console.log("Start ForEach");
    // your code
    await wixData.update("Database", toUpdate);
    console.log("Database Update");
    // your code
    console.log("End ForEach");
}

I think this may help.

Or another way.
To be sure that some code will run AFTER update(), you can use .then() instead of await. And put your code inside then()

export function button_click() {
    // your code
    console.log("Start ForEach");
    // your code
    wixData.update("Database", toUpdate)
    .then( () => {
        console.log("Database Update");
        // your code
        console.log("End ForEach");
    })
}