Dumb While Loop Question

I’m trying to run a simple loop to pull in some images to a collection from URLs

I am pulling 10 results from a collection and assigning them to the review variable. This works fine. I then want to pull each record in the collection in turn using a while loop. I had the code written for that, which I understand, but I cannot get the loop to see the review variable…is there something I need to do to pass the variable to the loop? I pulled the other code out to see if I could just log the variable in the console.

The first two console.logs work fine. The third just outputs "Array to process: [object Object] " in the console.

import wixData from 'wix-data';

$w.onReady( async function() {

let reviews;
await wixData.query("Books")
    .then((results)=> {
    reviews = results.items;


let i = 0;
while (i < 5) {
    console.log("Array to process: "+reviews[i]);


Haven’t done a lot with loops until now, so haven’t see this before…sorry if it seems like a total noob question…


Hi Simon,

The code is fine by itself,
The only problem you have is that you run your query on the wrong place
or you are running the while loop on the wrong place :slight_smile:

What happends now is that the while loop runs first.
evrything in the $w.onReady will run when the page is loaded.
evrything straight in the code page might run before it.

So there are a few things you can do now.
You can make a function where you run the query and the while loop and run the function in the $w.onReady.
You can also use the following code

await wixData.query("Books")
let res =results.items;
return res
console.log("Array to process: "+newRes[i]);

Hope this answers your question :slight_smile:

and no worries about the “noob question”
Evryone had to begin at that stage :slight_smile:

Kind regards,