Retrieve items from query and assign to Repeater

Hi everyone , Good morning !!

What I am trying to do is to do a query of Database and loop each result.
(I have created a post similar to this but accidentally deleted :sweat_smile:)

After the query when the result is obtained, I want to assign the latter to the repeater.
Here is my code →

wixData.query("Messages")
  .eq("userId", userId3)
  .or(
    wixData.query("Messages")
      .eq("userUserid", userId3)
  )
  .eq("userId", userId2)
  .or(
    wixData.query("Messages")
      .eq("userUserid", userId2)
  )

 
  .find()
  .then( (results) => {
 if(results.items.length > 0) {
 let items1 = results.items;

 let allMessages = []
 let allId = []
 for (var i = 0; i < results.items.length; i++) {
                allMessages[i]=[items1[i].message]
                allId[i] = [items1[i]._id]
                console.log(allMessages)
 
     $w('#repeater1').data = allMessages[i];
console.log("All id with user  is " + allId[i] + " " + allMessages[i]);

 

 
   } 
   }
   } );

The result obtained is →
(Page)(Repeater)

(Console → )

Thanks,
Ajith

You need to set the data property of the Repeater to an array of items. Do something like this after building your array of items:

$w('#repeater1').data = allMessages;

Make sure that one of the item fields is _id, which should be a unique value for each item.

Also, you will need an onItemReady() function to assign the item values to the appropriate screen element.

Refer to the Repeater documentation on how to properly work with a Repeater.

Israel what do you mean by that?

Make sure that one of the item fields is _id , which should be a unique value for each item.
new unique data-array which includes all the messages and the corresponding IDs?
Makes sense!

Something like this?

let myNewData = []

 for (var i = 0; i < results.items.length; i++) {
      myNewData.push(items._id, items.message)
});

$w('#repeater1').data = myNewData;        

Hey @russian-dima , The repeater expects the data to have a unique id for each item. See the data property API for some code examples and an explanation.

@yisrael-wix
Then my code example should work normaly, right?
Of course you have to add the filter-code.

And as i understand, you have first to RESET the REPEATER-DATA first, before input new data?

At least you have to create a two-dimensinal array for it, right?

let myNewData = []

 for (var i = 0; i < results.items.length; i++) {
      myNewData.push(items._id, items.message)
});

$w('#repeater1').data = []; 
$w('#repeater1').data = myNewData;   

@russian-dima I don’t know what the variable items is, but the code would be more like:

myNewData.push({"_id": items[i]._id, "message": items[i].message));

In any case, something like that. See the code snippets in the API.

@yisrael-wix
Yes i understand, it’s an object, not even just a 2-dimensional array anymore.
Ok, makes sense. Thx.

Thanks Everyone !!!
The error is not showing any more !!!

What is your result-code?

@yisrael-wix

Thanks Yisrael, i could reconstruct and analyse all about the DISTINCT-function.
Little corrrection:

myNewData.push({"_id": items[i]._id, "message": items[i].message));

myNewData.push({"_id": items[i]._id, "message": items[i].message});

Here my thoughts about DISTINCT… (something wrong or missing in my work?)

https://russian-dima.wixsite.com/meinewebsite/test-test-test

Who is looking for how is working DISTINCT, he should perhaps also take a look on this post here…