Question:
I’m trying to filter the data in an existing dataset depending on the Page URL that the user lands on, and use the filtered data to populate a Repeater.
Problem is with the filtering variable not working out.
What I’m trying to achieve:
Each user is directed to a dynamic page with a Repeater.
An existing Dataset with 10 items is then filtered with the field name ‘tagger’ that shows value ‘1’ for the first 5 items (these items are shown through the Repeater to the users landing on ‘/url1’) and value ‘2’ for the next bunch (items shown through the Repeater to users landing on ‘/url2’).
What I’ve tried:
Need to admit, I’m not too knowledgeable in coding, and most of the code I’ve gathered was from the discussions here and Wix documentation, along with a lot of help from Velo Assistant.
I’ve set up a field name ‘tagger’ in ‘mydataset’ with the value ‘1’ for the first 5 data items, and value ‘2’ for the next 5 items. Tried to set up the variable ‘taggerValue’ so that the dataset can be filtered depending on the value, but right from the start, the ‘taggerValue’ is going undefined.
On previewing url1 page, the first console log right before the ‘try’ block returns “url1 undefined”. The Repeater is not visible on the page at all. This is the code I’ve used –
import wixData from 'wix-data';
import wixLocationFrontend from 'wix-location-frontend';
$w.onReady(async function () {
let taggerValue;
const url = wixLocationFrontend.path[0];
function tagging(url, taggerValue) {
switch(url) {
case '/url1':
taggerValue = 1;
return (url + taggerValue);
break;
case '/url2':
taggerValue = 2;
return (url + taggerValue);
break;
default:
return url; // Exit if URL doesn't match any case
} }
console.log(tagging(url + " " + taggerValue));
try {
//const filter = wixData.filter().eq('tagger', taggerValue);
//const sort = wixData.sort().ascending('sNo');
const query = wixData.query('mydataset').eq('tagger', taggerValue).ascending('sNo');
const results = await query.find();
console.log(results.items);
$w("#listRepeater").data = results.items;
$w("#listRepeater").forEachItem(($item, itemData, index) => {
$item('#numberText1').text = itemData.number;
$item('#titleText1').text = itemData.title;
$item('#subText1').text = itemData.subText;
});
} catch (error) {
console.error("Error fetching data:", error);
}
});
-------------
I’ve been banging my brain for 3 days to get till here and I’m now on an urgent deadline. Please, if anyone could help me understand what I’m missing to get the filtering to work, or if I’m using something incorrectly, I’d be so grateful.