Help building list of Collection _id values

Hi all.

I am trying to extract of all the individual IDs in a collection, to use elsewhere to process the content of the collection.

Here’s my current code:

import wixData from 'wix-data';

$w.onReady( async function() {

let results = await bigQuery();
var item_Id_list = [];

console.log(results);
console.log(results.items[1]._id);

for (var i=0;i<10;i++) {
    item_Id_list[i] = results.items[i]._id;
}

console.log(item_Id_list);
})

async function bigQuery(){
for (var i=0;i<10;i++) {
    let results = wixData.query("BookReviews")
      .find();
    return results;
}
}

The first console.log gives me the expected object and the second gives me the ID that I was after, from the second item in the collection (added this laterm in an effort to understand why I can’t build the array.

When I run the for loop, I get the following error:

Why doesn’t this work? Can’t see how its a timing issue, as the results is appearing in the console as you’d expect and I have the await function there on the query. So maybe I am just trying to read the object wrong??

Haven’t done this before and have been reading to try to figure it out but to no avail…any help would be appreciated!

Your bigQuery function is returning all of the items in your collection for each iteration of the loop. You can get all the items in the collection by chaining your wixData query with a .then statement. Something like this:

let reviews
await wixData.query("BookReviews").find().then((results)=> {
    reviews = results.items
    console.log(reviews)
})

You also do not have to use a separate function for this (unless you want to for some reason). You can instead just fetch this data directly from the onReady function that fires on page load.

Doing this will get you an array of all the reviews stored in the Book Reviews dataset.

Perfect…thanks.