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;
});
}
})