Query forEach Skips items

Hi everyone,

I created a script to update some values on a collection so that they have currency format, however when running the script it skips some of the items, any idea why?

P.S. I have tried doing it by adding a limit of 50 and the first query works fine, but after that and skipping to the next 50 it starts to skip items.

Here is the script:


export function button17_click(event) {
 //perform();
    $w("#gif").show();
    updatetotals();
}

async function updatetotals() {
 const result = await wixData.query("InvoiceHolding").skip(0).limit(50).find();

    result.items.forEach(async function (item) {
            item.totalformato = formatoprecio(item.total);
            item.pagoformato = formatoprecio(item.pago);
 await wixData.update("InvoiceHolding", item)
    });
    $w("#gif").hide();
    $w("#table1").refresh();
}

function formatoprecio(x) {
 var number = Number(x);
 return '$' + number.toLocaleString('en-US', {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
    });
}

Any ideas?

One possibility is that it’s trying to run when the query promise isn’t yet fulfilled, so you would restructure your first line in updatetotals like this

 async function updatetotals() {
   await wixData.query("InvoiceHolding").skip(0).limit(50).find().then(async function(result) { 

Another is that the forEach may not be playing well with the async update for some reason, which you could resolve by using a loop instead of forEach.

I would guess the first option is much more likely, but sometimes stuff is just crazy.