How do I do a query loop and populate a repeater

I have an array that is retrieved from a previous code, the array length and content is not predictable. I want to loop through that array and use each element of the array to filter a query and fill repeater containers. Basically how do I get the index of each container in a repeater and replace it with the loop index and fill the specific container with the result of the loop query that pertains to the container.

arrayy = [a,b,c,d]
for(let i=0; i<arrayy.length;i++){
let fill = wixData.query(“ALL”)
.eq(“allid”,array[i])
Promise.all([fill.limit(1000).find()])
.then((res)=>{
let returned = [res[0].items].flat()
$w(“#datasetfill”).setCurrentItemIndex(i) = returned//This where I have the problem, repeater is connected to this dataset
$w(“#repeaterfill”).container= returned//Or can I populate each container based on there index value?
})
}//end of loop
The populated repeater is expected to have the number of containers as the arrayy length

  1. First you do your query-loop
  2. then you fill your REPEATER with the RESULT-DATA.

Your RESULT-DATA will have to be an ARRAY or ARRAY of OBJECTS.

All informations you need you will get here…
https://www.wix.com/velo/reference/$w/repeater/introduction

arrayy = [a,b,c,d]
$w(“#datasetfill”).onItemReady(($item,itemData,index=>{
$item (“#Title”). text= itemData . title ;
}
for(let i=0; i<arrayy.length;i++){
let fill = wixData.query(“ALL”)
.eq(“allid”,array[i])
Promise.all([fill.limit(1000).find()])
.then((res)=>{
let returned = [res[0].items].flat()
$w(“#datasetfill”).data[i] = returned
})
}//end of loop, This doesnt work

@chidiejike
Can you show your DATABASE (or an excerpt of it) with the related DB-FIELDS?
Your code will not work, like that.

  1. First ERROR already in your first code-line…
arrayy = [a,b,c,d]

where did you define your → “arrayy” ???

a, b, c ← has been defined somewhere? If not → it should be → “a”, “b”, “c” as STRINGS!

  1. Second ERROR in second line …
$w("#datasetfill").onItemReady(($item,itemData,index=>{
	$item("#Title").text= itemData.title;
}

You are using code for DATASET, which was created for using for REPEATERS!
Or is → #datasetfill ← the ID of your REPEATER ???
Not logical & systematical coding-practise !

Without to observe the rest of your code, first show a screen of your DB and tell me what you want to achieve?

I will make the required corrections but can the results also be used to determine the items of the dataset within the loop?

Yes it is possible! But i think you will have also other possibilities.
I assume you are trying to filter an ARRAY-DATA inside your DB or a SELECTION-TAG-FIELD or something like that, right?

If so, there are also other methods and techniques how to do it.

Thanks for the response
within this as the loop fills each container data with the looped objects inside the items array, how do I change the container color within the same loop?
I am trying to filter the items array inside the DB
results = {items:[{colarowa,colbrowa,…},{colarowb,colbrowb,…},{colarowc,colbrowc}]}
I am trying to loop through objects inside the items array that fit a query criteria so that the returned objects will populate the repeater containers. The criteria and how many times the loop will occur is not specific, it will be determined by the length of the array and contents.
As you may know the contents of the array are ids that are compared with the “allid” in the “ALL” DB

let arrayy = [a,b,c,d]
$w(“#repeaterfill”).onItemReady(($item,itemData,index=>{
$item (“#Title”). text= itemData . title ;
}
for(let i=0; i<arrayy.length;i++){
let fill = wixData.query(“ALL”)
.eq(“allid”,array[i])
Promise.all([fill.limit(1000).find()])
.then((res)=>{
let returned = [res[0].items].flat()
$w(“#repeaterfill”).containercolor = “red”// something like this
$w(“#repeaterfill”).data[i] = returned[i]
})
}

Looping trough Objects…

const object1 = {
  a: 'somestring',
  b: 42
};

for (const [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}

And please start to use CODE-BLOCKS when you show your code!


It’s not that difficult. Thanks!

Maybe you did not get what I am trying to do, I only need to loop through the array of objects and fill the repeater with whatever is contained in the object as I have already connected the objects data(itemData) to all the elements in each container of the repeater. What I don’t get is how to get the index of each container that is looped(not the data inside) and change the background color for each container per loop.

Yeah perhaps i did not understand you right.
Perhaps you will need a loop in a loop.

let arrayA = ["1","2","3","4","5"];
let arrayB = ["a","b","c","d","e"];

for (let a = 0; a < arrayA.length; a++) {
  for (let b = 0; b < arrayB.length; b++) {
     console.log(arrayA[a]+arrayB[b]);   
  }   
}

Also take a look onto this example, to be found here in this parallel running post…


Perhaps thiss will show you how to work with ARRAYs in combination with OBJECTs.