Filtering a Dataset based on the Page URL - URGENT

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.

You defined the function tagging as requiring two variables url and taggerValue

Yet you call that fun tion, providing a single argument with the value url + " " + taggerValue

By the way, being ran on a specific page, would the url path ever be different? Might you want to use URL queries instead?

https://example.com/path/to/page?queryKey1=value&queryKey2=value

1 Like

Thank you for responding, Dean!

If you’re talking about the line -
console.log(tagging(url + " " + taggerValue));

I had to place it that way. I tried to split them with a comma, the console was showing it as an error. If there’s any other way to pose this, please let me know. I will test it out. Most of this code was done in a trial & error way as I’m not much knowledgeable in coding.

The URLs would be different because this is meant to be a Dynamic page. Originally, it has 30 different item pages, so we get 30 different URLs.

If this helps for context – user lands here by answering two different radiogroups ~ one with 5 choices & the other with 6 choices. Combination of the 5 & 6 choices - we get our 30 pages. Which is why my primary goal is to make this as easy as possible by storing all 30 items (with 5 sub-items each coz the Repeater’s supposed to have 5 items too, with the “number”, title & subtext from the screenshot all being text boxes) into one dataset and give each of the 5 sub-items a tagger.

I could figure out the data linking part, but the filtering part is giving me problems and I’m going nearly mad trying to solve this.

That being said, I don’t know much on how to use URL Queries. If that would help this particular issue, please let me know how I can go about implementing them. Any help would be appreciated. Thank you so much!

Based on this code, it looks like you are not using the dataset filtering function whis is potentially the issue. You have it in your code but it’s commented out and you are instead using wix data to populate your repeater via code, thus causing a collision between datasets and wix data likely.

I would take a look at this video on how to build a dataset filter object and see if it helps clear things up for you

1 Like

Is this a dynamic single item page with a repeater?

Or is this a category dynamic page with a repeater?

Dynamic category pages work specifically to display a filtered list of items in a repeater based on the category (URL / field). It’s a logic thing. Not a code thing. No code needed.

3 Likes

Thank you for your response, @codequeen .
I didn’t know we could use the categories as a separate page way. I’ll have to try it out. For the current task, doing this means changing so much of my current layout. And I need to check if the choosing can be automated as the user basically lands on the page and goes through the info available to them without needing to change pages or interact much. But, thank you for sharing about Dynamic categories. I appreciate the help!

If all you need is to know which product you’re on, you can get it from the dynamicDataset itself

$w('#dynamicDataset').onReady(() => 
    const item = $w('#dynamicDataset').getCurrentItem()
)

Hello @amandam,
I was using the taggerValue variable in the line
const query = wixData.query('mydataset').eq('tagger', taggerValue).ascending('sNo');

to “filter” my dataset as I couldn’t exactly use filter with the query.
But, you might be right, there might be some sort of collision happening there.

Thank you so much for sharing the video!
On first look, it seemed completely different to my requirements, but I was able to finagle something that works for me coz you’re right, the base was similar.

This is my updated code :

import wixData from 'wix-data';
import wixLocation from 'wix-location';

$w.onReady(async function () {
	$w('#listRepeater').onViewportEnter(() => {
		updateFilter();
	})
});


function updateFilter() {

	const url = wixLocation.path.join('/');
	let filter = wixData.filter();
	
	if (url) {
		filter = filter.contains('url' , url);
	}
	
	$w('#mydataset').setFilter(filter);

}

And it’s working so far! Aaand it’s so short and succint and simple!! Thank you for saving me. You are an angel. Just one small issue with the current code – I’m getting an error when I try to Publish –

The build log shows an error on a statement that used to be there, but doesn’t exist anymore –

Apparently, I can ignore all this and just Publish if I have to, but I want to be sure that this isn’t something serious… and if there’s a way to clear the past error on the build log. I couldn’t find anything helpful about this online.

Anyways, thank you so much for your help!

1 Like

Hello @DeanAyalon,

Thank you for replying.
No, the objective is to send them to a specific page depending on their choices on the previous pages. And each page has a repeater which is connected to another dataset that needs to be filtered acc. to the page the user lands on.

I was able to update my code acc. to other replies here, and this code so far is working –

import wixData from 'wix-data';
import wixLocation from 'wix-location';

$w.onReady(async function () {
	$w('#listRepeater').onViewportEnter(() => {
		updateFilter();
	})
});


function updateFilter() {

	const url = wixLocation.path.join('/');
	let filter = wixData.filter();
	
	if (url) {
		filter = filter.contains('url' , url);
	}
	
	$w('#mydataset').setFilter(filter);

}

Only problem now is that I’m getting this when I try to Publish –

And the build log shows an error on a statement that used to be there, but doesn’t exist anymore –

I could ignore this and just Publish, but I want to make sure this isn’t something serious and clear the past error on the build log, if it’s possible. I couldn’t find anything helpful about this online.

Appreciate all your help on this, though. Thank you so much!

Solving the error is also extremely simple
Somewhere in your code, you have an await keyword, that can only be used within asynchronous functions
Simply define the function it’s used in as asynchronous

But then again, I don’t think I quite understand exactly what your goal is, if you could, please explain it in more detail, perhaps the entire approach is from the wrong direction. (Although, you know, “If it works, it works”, if it’s urgent have at it)

Thank you, @DeanAyalon .

I did check through the updated code and it doesn’t have ‘await’ anywhere.
One of the previous iterations used to have it, but I’ve removed it and changed the code. I’ve also added async to onReady, just to be safe, but there’s no change in the error on the log. The Developer console shows no errors on preview, either.

This is the current code -

import wixData from 'wix-data';
import wixLocation from 'wix-location';

$w.onReady(async function () {
	$w('#listRepeater').onViewportEnter(() => {
		updateFilter();
	})
});


function updateFilter() {

	const url = wixLocation.path.join('/');
	let filter = wixData.filter();
	
	if (url) {
		filter = filter.contains('url' , url);
	}
	
	$w('#mydataset').setFilter(filter);

}

I suspect that the error on the build log is from a previous run, as well and it might not have updated. I don’t know how to erase or rectify it, though… since we technically don’t have an ‘await’ in the code.

Interesting, so long as you made sure it is nowhere in your code, it’s fine then, worst case - it can always be fixed ¯\_(ツ)_/¯

1 Like

Hello! If there seems to be some issue with being able to publish the most current version of your code you can wait a bit and try again, clear your cache (i would save your most recent code just in case), or potentially contact customer care.

I have seen one other report recently of the code getting out of sync that cleared up for the other user so perhaps this all isn’t relelvant anymore.

I will report that this happened to you but if you are blocked right now you may want to get a ticket started with customer care so they can investigate as well

Let me know if I can help more!

2 Likes

Thank you for your response, @amandam .

Can you please tell me how I can go about clear my cache for the code?

Thank you, again!

Browser settings, if you’ve a search function in them. look for “cache”

2 Likes

See what @DeanAyalon mentioned although if this is still happening and that code snippet is definitely gone, I highly recommend opening a ticket as well

1 Like

Thank you, @DeanAyalon. I thought there was a separate way to clear Wix logs, so had to ask. I did this in the meantime, though. It hasn’t worked in clearing the error. Logged a ticket as per @amandam 's suggestion. You’ve been a great help throughout this journey. Thank you!

1 Like

Thank you, @amandam . I cleared my browser cache, but it didn’t help with the error. Logged a ticket as per your suggestion. Thank you so much for helping me with all of this!

1 Like