Issue with repeater - query using contains & distinct

Hello, I am trying to populate a repeater using a query, filtering with contains and using distinct to avoid duplicates, but finally the repeater disappears (comment //codingissue). A hand would be great

import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import {session} from 'wix-storage';

let userEmail;  
let userId;
let titnomcirc

$w.onReady(function () {

wixData.query("Members/PrivateMembersData")
  .eq("_id", wixUsers.currentUser.id)
  .find()
  .then( (results) => {
    userEmail = results.items[0].loginEmail;
    userId = results.items[0]._id;
  $w('#text72').text = userEmail;
  });

  titnomcirc = session.getItem("flagcircuito"); 
  $w('#nomcirc').text = titnomcirc  

//CODINGISSUE
  $w("#repeater1").onItemReady( ($item, itemData, index) => {
    $item("#nomcateg").text = itemData.nomcateg;
 
  } );

wixData.query("clubbitComcategprod") 
    .contains("nomcom", $w('#nomcom').text)
    .contains("nomcirc", $w('#nomcirc').text)
    .distinct("codcateg")
    .then( (results) => {
      $w('#repeater1').data = results.items; 
 
  })
  .catch( (error) => {
 let errorMsg = error.message;
 let code = error.code;
  })

}) 

@noanest When the distinct function is used, it returns results only for that field, “codcateg” in this case. Consequently, the onItemReady is trying to assign the value of a field “nomcateg” that is not in the results.

@tony-brunsman thanks for replying!..I changed " . distinct ( “codcateg” )"
for " . distinct ( “nomcateg” )", as it is what the onItemReady expects, but it’s still not working. Also repeater continues to disappear on preview mode…

Probably I would end up using dropdowns instead of repeaters as I found more docs on how to populate and filter them, also avoiding duplicates, but It would be great to understand what I am missing in my code here. Thanks again

Although I tried to solve the problem, I’m still stuck in populating the repeater without duplicates. The code works fine without distinct(), but when I use it to avoid duplicates it will not show anything.

I think it might be related with how distinct () function returns field values and I should change something when I populate the repeater, but I just can’t find the way

Any idea will be great

You should perhaps show the structure of your databse. So it will be surely easier to work with this issue.

@russian-dima thanks for answering. Check this out:

The query works fine when I use find() , although the repeater is populated with duplicates

But when I use distinct() , the code shows me this error…

“Wix code SDK error: Each item in the items array must have a member named _id which contains a unique value identifying the item”

I tried using forEachItem, but still stuck


   $w("#repeater1").onItemReady(($item, itemData, index) => {
 /*$w("#repeater1").forEachItem(($item, itemData, index) => {*/
    $item("#text71").text = itemData.nomcateg;

  } );

  wixData.query("clubbitComcategprod")
 /*.eq("nomcom", "Prim Tpo")*/
    .distinct("nomcateg")
 /* .find()*/
    .then( (res => {

    $w('#repeater1').data = res.items;  
 
  }))


@noanest

import wixData from 'wix-data';

$w.onReady(function () { myFunction(userId) }); 

function myFunction () {
    wixData.query("clubbitComcategprod")
    .distinct("nomcateg")
    .then( (results) => {
 if(results.items.length > 0) {
 let items = results.items;
 let firstItem = items[0];
 let length = results.length;
        console.log(length)
        console.log(items)
        console.log(firstItem)

        $w('#repeater1').data = results.items;
        } else {   }
    })
    .catch( (error) => {
 let errorMsg = error.message;
 let code = error.code;
    });
}

-You have also checked DATABASE-PERMISSIONS?
-Do you get RESULTS in CONSOLE?

@russian-dima thank you very very much.

I see CORRECT RESULTS IN CONSOLE, but still have an error message. and the repeater is not populated. Database permissions are ok

I paste the error message and the entire code below.

ERROR MESSAGE
Wix code SDK error: Each item in the items array must have a member named _id which contains a unique value identifying the item.

// For full API documentation, including code examples, visit https://wix.to/94BuAAs

import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import { session } from 'wix-storage';

let userEmail;
let userId;
let titnomcirc

$w.onReady(function () {

    wixData.query("Members/PrivateMembersData")
    .eq("_id", wixUsers.currentUser.id)
    .find()
    .then((results) => {
      userEmail = results.items[0].loginEmail;
      userId = results.items[0]._id;
      $w('#text72').text = userEmail;
    });

titnomcirc = session.getItem("flagcircuito");
  $w('#nomcirc').text = titnomcirc

 //$w("#repeater1").onItemReady(($item, itemData, index) => {
 //$item("#text71").text = itemData.nomcateg;

 // });



//CODE ADDED FROM YOUR REPLY
//I already have an onready, I'm not sure how to fix it

$w.onReady(function () { myFunction(userId) }); 
function myFunction () {
    wixData.query("clubbitComcategprod")
      .distinct("nomcateg")
      .then((results) => {
 if (results.items.length > 0) {
 let items = results.items;
 let firstItem = items[0];
 let length = results.length;
          console.log(length)
          console.log(items)
          console.log(firstItem)

        $w('#repeater1').data = results.items;

        } else {}
      })
      .catch((error) => {
 let errorMsg = error.message;
 let code = error.code;
      });
  }

});

@noanest
Damn! The same here…

https://www.wix.com/corvid/forum/community-discussion/retrieve-all-items-from-other-field-after-query

Is it me, who make a MISSTAKE ?:persevere:

Take a look at this example…

https://russian-dima.wixsite.com/meinewebsite/repeater-dropdown

It works, without any problem.

$w('#repeater1').data = results.items

You have to DELETE 1x —> onReady() and put everything in just 1x-onReady().
Something like that… → ATTENTION! —> New CODE-VERSION!

import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import { session } from 'wix-storage';

let userEmail;
let userId;
let titnomcirc

$w.onReady(function () { 
    titnomcirc = session.getItem("flagcircuito");
    $w('#nomcirc').text = titnomcirc;
    get_UserData(); 
    eleminate_Dublicates(); 
}); 


function get_UserData () {
  wixData.query("Members/PrivateMembersData")
    .eq("_id", wixUsers.currentUser.id)
    .find()
    .then((results) => {
      userEmail = results.items[0].loginEmail;
      userId = results.items[0]._id;
      $w('#text72').text = userEmail;
    });
}


function eleminate_Dublicates() {
    wixData.query("clubbitComcategprod")
    .distinct("nomcateg")
    .then((results) => {
 if (results.items.length > 0) {
 let items = results.items;
 let firstItem = items[0];
 let length = results.length;
            console.log(length)
            console.log(items)
            console.log(firstItem)
            $w('#repeater1').data = results.items;
        } 
 else {}
    })
    .catch((error) => {
 let errorMsg = error.message;
 let code = error.code;
    });
});

@russian-dima

The same code that I pasted before, but using find() instead of distinct() works OK. So I think the problem is related with how distinct() retrieves the data, and that’s why I get an error message

If you have some code where you use distinct() when populating a repeater would be usefull

Thanks again

@noanest
Did you test the last version ?
The same results?

@russian-dima yes sr, the last code pasted was the one tested

  • when using distinct() the code shows the error message
  • when using find() works OK, BUT populates the repeater with duplicates

having a code that uses distinct() and populates a repeater (not a dropdown, as a wix example I saw) would help me I think to solve the issue

@noanest
try to do some console-log-analyses…

console.log(results.items) ---> using .find()
console.log(results.items) ---> using .distinct()
------or-------just-------results------
console.log(results) ---> using .find()
console.log(results) ---> using .distinct()

What are the both RESULTS showing in the CONSOLE ?
Are the both RESULTS identic?

Edit: I took a closer look onto the APi, and found this…

distinct( )

Returns the distinct values that match the query, without duplicates.
Description
The distinct() function returns a Promise that resolves to:

  • The distinct values found in the specified field when running the query.

  • Additional information about the results, such as the number of values that match the query.
    Unlike find(), which returns all item objects that match the query, distinct() returns matching field values, and eliminates duplicate field values from the query result. You cannot use find() and distinct() together.
    For an item to be resolved as distinct, only the specified field must be distinct. Other fields for that item in the collection are not evaluated when resolving the promise.
    The Promise is rejected if distinct() is called with incorrect permissions or if any of the functions used to refine the query is invalid.

So now it is interessting which result-structure gives a “distinct-query-result” back?

Can you do the test and show the results ?

@russian-dima

I copy console.log images for running find() and then distinct(). When I use distinct() there is an issue with the _id but I don’t know how to solve it

CONSOLE.LOG RUNNING CODE WITH FIND()
…you can see the first repeater populated, but with duplicates that are not seen in the image

CONSOLE.LOG RUNNING CODE WITH DISTINCT()
…takes the options but when tries to populate the repeater comes the _id error

What do u think ?

@noanest
I think i have to reconstruct it myself tommorow, to understand better the issue.
To be honest i never used “distinct” before, so i have first to analyse it myself.

@russian-dima it’s ok man, u did enough already in helping me.

I can move on by changing data structure and avoid duplicates, but I would like to understand why find() works and distinct() doesn’t

If not using distinct(), do you know any other way of dealing with duplicates before populating a repeater ?

Thanks

@noanest
Yes there is surely another way, how to solve it, but today i am to tired to think of any issue. And there is Wendy on other posts, who wants to know about everything :grin:.

So, i am out for today! See ya tomorrow :wink:.

@russian-dima thank u very much for the time u are taking on this. See ya

@noanest
This could be interesting for you…

https://www.wix.com/corvid/forum/community-discussion/how-to-create-dropbox-checkbox-options-from-a-database-array-tags-field

One more post to check for you ^^
https://www.wix.com/corvid/forum/community-discussion/use-dropdown-without-duplicates-with-dataset-and-repeater