Query in a FOR loop

hi
i have a FOR loop and inside it im trying to query my database.
the criteria for the query rely on the FOR loop value (for example ‘i’ value)

the results im getting are incorrect and i think its because it is async so the loop ends before the query start.

how can i force the query and the loop work sync or what is the fix for this?

here is an example:

for (var i=0;i<20;i++) {
wixData.query(“db”)
.limit(1000)
.eq(“reg”, regNames[i])
.ge(“_updatedDate”, startdate)
.le(“_updatedDate”, enddate)
.find()
.then(results => {
console.log("res len: " + results.length + " i: “+i+” reg: " + regNames[i] + " start date: " + startdate.toLocaleDateString() + " end date: " + enddate.toLocaleDateString());
data[i]= results.length;
});
}

the result i get is:
res len: 0 i: 20 reg: aaa start date: 10/12/2018 end date: 11/12/2018
res len: 4 i: 20 reg: aaa start date: 10/12/2018 end date: 11/12/2018
res len: 3 i: 20 reg: aaa start date: 10/12/2018 end date: 11/12/2018
res len: 8 i: 20 reg: aaa start date: 10/12/2018 end date: 11/12/2018
res len: 2 i: 20 reg: aaa start date: 10/12/2018 end date: 11/12/2018
res len: 1 i: 20 reg: aaa start date: 10/12/2018 end date: 11/12/2018
res len: 0 i: 20 reg: aaa start date: 10/12/2018 end date: 11/12/2018
res len: 9 i: 20 reg: aaa start date: 10/12/2018 end date: 11/12/2018
.
.
.

any help?

1 Like

You can use await, something like this:

for (var i=0;i<20;i++) {
   let results = await wixData.query("db")
      .limit(1000)
      .eq("reg", regNames[i])
      .ge("_updatedDate", startdate)
      .le("_updatedDate", enddate)
      .find();
}

Disclaimer: The above code wasn’t tested. However, it should get you going in the right direction.

So results would be an array that contains the different items from the query?

Yes, results contains an items array for the WixDataQueryResult .

To show how to build an array of results from all of the queries, let’s use the above code that I posted with some modifications:

let allItems = [];
for (var i=0;i<20;i++) {
   let results = await wixData.query("db")
      .limit(1000)
      .eq("reg", regNames[i])
      .ge("_updatedDate", startdate)
      .le("_updatedDate", enddate)
      .find();
      
   let items = results.items;
   // Now you can do something with these items
   // before continuing with the loop.
   // For example, add all query items to one big array.
   let allItems = allItems.concat(items); // add items from query
}

I hope this helps,

Yisrael

Thank you very much

Hi @yisrael-wix ,

How to concat results array like you did for results.items? . I need to have the whole results array in the for loop combined to get the .query, number of pages , nextSkipNumber,prevSkipNumber etc… Thanks for a solution…

when im trying this - i get an error because await must be used in async function.
so i cannot use it in the button function.
so i made another function and its working. i still get the data in parts, adding up line by line to the table on my form… not all the data in once! i cant understand how can i pause all execuation and only when i have all the data - then update the table or list on my screen. (or maybe this is how the table itself works?? the table adds the data row by row even when the data delivered all in once?)

the idea is the user have to wait until all the data displays and he dont know when its done and all the data is finished loading.
he can click a button in the middle while data is being loaded into the table.