Trying to filter a dataset by a field with multiple elements

Dear Wix community,

I have a dataset containing questions which have a field “categories”, where I can choose one or more categories that match the question. This is a reference to another dataset called “CategoryQuestions”.
Now on my page, I want to be able to filter questions by one category. I’ve found a tutorial where you show how to filter by a referenced element but this doesn’t seem to work once i enabled “multiple” on that categories field.

The code i have to filter it is the following:
function filter(text, category) {
if (lastFilterText !== text || lastFilterCategory !== category) {
let questionsFilter = wixData.filter();
if (text) {
questionsFilter = questionsFilter.contains(‘title’, text);
}
if (category) {
questionsFilter = questionsFilter.eq(‘categories’, category)
console.log(category);
}
$w(‘#questionsDataset’).setFilter(questionsFilter);
lastFilterText = text;
lastFilterCategory = category;
}
}

I’ve tried multiple things for the line questionsFilter = questionsFilter.eq('categories', category) but nothing seems to work.

Any chance someone might now how that works?
Many thanks,
Kim

Hello

The problem will probably be in .eq as it looks for exact matches. try using .contains instead. - here

I hope this solves it !
Massa

Hi Massa,

Thanks for your reply - I’ve tried that as well, no luck either unfortunately. I’ve just tried adding a new field called categorySingle in order to boil the issue down. It’s also a reference field like categories but cannot have multiple items. And that works perfectly.

Here’s the database:

The properties for the fields in question

Furthermore, even when printing itemData in my repeater, the categories field does not show up (output of console.log of itemData[‘categories’], itemData[‘categorySingle’] and entire itemData)


And here the code

Got it to work using https://www.wix.com/code/reference/wix-data.WixDataQuery.html#hasSome :slight_smile: