Ascending & distinct

Hi,

I have this piece of code working fine, BUT when I try to use ascending with distinct(), it won’t work unless I use the same keyfield

I found this post (https://www.wix.com/corvid/forum/community-discussion/wixdata-query-fails-when-ascending-and-distinct-are-different-types) where a (using-find&eliminating-duplicates) alternative is offered, but I can’t make it work

I tried to write my code using the (find) alterantive, but repeater is not populating.

Help would be really welcome

// ORIGINAL CODE WORKING FIND, BUT NOT WORKING WHEN ADDING ASCENDING 
 
{loadFirstrepeater()}

function loadFirstrepeater () {

            wixData.query("clubbitComcategprod")
            .contains("nomCom", $w('#dynamicDataset').getCurrentItem().nomCom)
            .contains("nomCirc", titnomCirc)
            .distinct("nomCateg")
 //.ascending("nomCateg")
 //.find()
      .then((results) => {
 if (results.items.length > 0) {
 let items = results.items;
 let firstItem = items[0];
 let length = results.length;
 let query = results.query;
                    console.log(length)
                    console.log(items)
                    console.log(firstItem)
                    console.log(query)

                myNewData = []  
 for (var i = 0; i < length; i++) { 
            myNewData.push({"_id": ( i ).toString(), "nomCateg": items[ i ]})   
            }

console.log(myNewData)
//$w('#repeater1').data = [] // IS THIS LINE NECESSARY ??
$w('#repeater1').data = myNewData;

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

});

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

// NEW CODE USING ASCENDING & FIND, AND 
// (TRYING) TO ELIMINATE DUPLICATES & POPULATE REPEATER

{loadFirstrepeater()}

function loadFirstrepeater () {

            wixData.query("clubbitComcategprod")
            .contains("nomCom", $w('#dynamicDataset').getCurrentItem().nomCom)
            .contains("nomCirc", titnomCirc)
            //.distinct("nomCateg")
            .ascending("codCateg")
            .find()
       .then((res) => {
 if (res.items.length > 0) {
 let MyNewData = res.items.map(e => e.nomCateg);            //nomCateg 
              myNewData = res.filter((e, index) => res.indexOf(e) === index);
               console.log(res);
 
console.log(myNewData)
//$w('#repeater1').data = [] // IS THIS LINE NECESSARY ??
$w('#repeater1').data = myNewData;

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

});

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

First of all, in your first code the ascending has to be before the .distinct().

In your second code you have a mistake.
instead of:

myNewData = res.filter((e, index) => res.indexOf(e) === index);

Use:

myNewData = myNewData.filter((e, index) => myNewData.indexOf(e) === index);

[FIXED]

Thanks for replying !

In the first case, it was a copy&paste error

But in the second, it was a lack-of-knowledge error. I changed it, but still not populating the repeater


{loadFirstrepeater()}

function loadFirstrepeater () {

            wixData.query("clubbitComcategprod")
            .contains("nomCom", $w('#dynamicDataset').getCurrentItem().nomCom)
            .contains("nomCirc", titnomCirc)
            .ascending("codCateg")
 //.distinct("nomCateg")
            .find()
       .then((res) => {
 if (res.items.length > 0) {
 let MyNewData = res.items.map(e => e.nomCateg);            //nomCateg 
              myNewData = myNewData.filter((e, index) =>                       myNewData.indexOf(e) === index);
               console.log(res);
 
console.log(myNewData)
//$w('#repeater1').data = [] // IS THIS LINE NECESSARY ??
$w('#repeater1').data = myNewData;

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

});

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

@noanest It’s not working because of 2 things:

  1. You used M yNewData with a capital M and then you used m yNewData. you must be consistent, otherwise it will not work.

  2. A repeater accepts an array of objects while you tried to assign an array that only includes the nomCateg values (which I guess are only strings),
    So first you should decide what exactly you want to assign and then fix in accordance.
    For example let’s say you want to assign the objects as appear in your collection but you want to omit objects that have the same nomCateg. Then instead of:

let MyNewData = res.items.map(e => e.nomCateg);            //nomCateg 
myNewData = myNewData.filter((e, index) => myNewData.indexOf(e) === index);              

Do:

let items = res.items;
let myNewData = items.filter((e, i) => items.findIndex(c => c.nomCateg === e.nomCateg) === i);

@jonatandor35 sorry about the first omission, and thank you very much for the time you took on this. It works perfectly

Not that I have a great corvid coding knowledge, but java is really far from me right now

Thanks again