Removing duplicates with distinct() fails

My question revolves around the distinct() function and my inability to use it in a query. The following population of a table works fine except that duplicates are present.

export function table3_cellSelect_1 ( event ) {
var cellData = event . cellData ;
$w ( ‘#button1’ ). label = event . cellData

wixData . query ( “data2filter4” )
. contains ( “relatedlevel” , “2” )
. and ( wixData . query ( “data2filter4” ). eq ( “other” , cellData ))
. find ()
. then ( res => {
$w ( “#table2” ). expand ();
$w ( “#table2” ). rows = res . items ;

console . log ( res . items );
}); }

In order to eliminate the duplicates, I have been trying to add the distinct function and keep getting the error below. The log prints out an array using res.items . Below is one of my attempts that generates the following error:
" Wix code SDK error: The row parameter of item at index 0 that is passed to the rows method cannot be set to the value LEFT ELBOW. It must be of type object"

export function table3_cellSelect_1 ( event ) {
var cellData = event . cellData ;
$w ( ‘#button1’ ). label = event . cellData

wixData . query ( “data2filter4” )
. contains ( “relatedlevel” , “2” )
. and ( wixData . query ( “data2filter4” ). eq ( “other” , cellData ))
. distinct ( “target” )
. then ( res => {
$w ( “#table2” ). expand ();
$w ( “#table2” ). rows = res . items ;

console . log ( res . items );
}); }

What am I missing? I recognize that one can call a function to perform the deduplication but this appears to be simple answer to the duplicate issue.

I would suggest moving the console.log(res.items) statement to before setting the rows property, like this:

console.log(res.items); 
$w("#table2").rows=res.items;

This way you can see what’s returned by the query before your code crashes. Maybe you’ll see something in the returned data that will help you understand what’s happening.

I don’t know what your collection looks like, and I don’t know what’s being returned, but the error you’re getting seems to indicate that you are not getting a proper object from the query.

Not sure if it helps, but you can look at Example: Remove duplicates from connected dropdown options using distinct() query which does pretty much what you’re doing.

Apologies for not defining my collection Yisrael. There are 8 columns, all text. You’ll note that I filter on 2 different columns (related level and other). If I use find(), then I get the following duplicates for the field “target”. Shown result is truncated for brevity:

I then replace find() with distinct (( . distinct ( “target” )) and get the correct result-again result truncated for brevity:

But I get the error which refers to this line $w ( “#table2” ). rows = res . items ; :
The row parameter of item at index 0 that is passed to the rows method cannot be set to the value MR Pelvis Without Contrast. It must be of type object.

I hate it when I’m stupid. I think I see it now. The returned results from distinct() is an array and the Table’s rows property needs to be set to an object (not an array). You just need to convert the returned array into the required object format that Table.rows wants.

Not so fast Yisrael-it is I that is stupid! How does one convert the returned array into the required object format that Table.rows wants? You’re link does clarify it for me (newbie here!)

.//....
.distinct("target") 
 .then(res => { 
 $w("#table2").rows = res.items.map(e => ({target: e})); 
});

Of course it’ll only fill out the “target” column.
If you need more fields, you can’t use .distinct()

THANK YOU! Just so others might be helped here is my final code to get a table populated with the results of a query that includes a distinct() and finally to convert the query result to an object that the table can accept:

wixData . query ( “data2filter4” )
. contains ( “relatedlevel” , “0” )
. and ( wixData . query ( “data2filter4” ). eq ( “other” , cellData ))
. distinct ( “target” )
. then ( res => {
$w ( “#table4” ). rows = res . items . map ( e => ({ target : e }));
$w ( “#table4” ). expand ();

Grateful for your thoughts!

You’re welcome :slight_smile: