Can't get two filters to work at the same time

Good morning! Hoping there’s a simple solution to my problem :).

I easily created a filter with a set of check boxes, but when I tried to create a second filter to work together with the first, I realized I’m missing something. Might anyone be able to help me figure out what I can’t?

The site page is here: https://londonmiddlebury.wixsite.com/pqchcv1/programs-and-services

The yellow filter works, the green does not :frowning:

Thank you thank you thank you!

// For full API documentation, including code examples, visit http://wix.to/94BuAAs

import wixData from 'wix-data';

function AudienceFilter() {
 const checkedaudience = [
                $w('#checkbox1').checked && 'Adult',
                $w('#checkbox2').checked && 'Parent',
                $w('#checkbox3').checked && 'Senior',
                $w('#checkbox4').checked && 'Newcomer',
        $w('#checkbox5').checked && 'Young Adult',
        $w('#checkbox6').checked && 'Child',
        ].filter(Boolean)

 const query = checkedaudience.reduce(
                (query, audience, i) =>
                i === 0
                ? query.eq('audience', audience)
                : query.or(wixData.query('programsANDservices').eq('audience', audience)),
                wixData.query('programsANDservices')
        )

        query.find().then(results => {
 //console.log(results.items);
 let programsANDservices = results.items
                $w('#serviceRepeater').data = programsANDservices
        })
}

$w.onReady(function () {
        AudienceFilter();
});

export function checkbox1_change(event, $w) {
 //Add your code for this event here:
        AudienceFilter(); 
}

export function checkbox2_change(event, $w) {
 //Add your code for this event here:
        AudienceFilter();  
}

export function checkbox3_change(event, $w) {
 //Add your code for this event here: 
        AudienceFilter(); 
}

export function checkbox4_change(event, $w) {
 //Add your code for this event here:
        AudienceFilter();  
}

export function checkbox5_change(event) {
 //Add your code for this event here: 
        AudienceFilter(); 
}

export function checkbox6_change(event) {
 //Add your code for this event here: 
        AudienceFilter(); 
}
function ServiceFilter() {
 const checkedservice = [
                $w('#checkbox7').checked && 'Health',
                $w('#checkbox8').checked && 'Housing',
                $w('#checkbox9').checked && 'Education',
                $w('#checkbox10').checked && 'Mental Health',
                $w('#checkbox11').checked && 'Parenting',
                $w('#checkbox12').checked && 'Child Care',
                $w('#checkbox13').checked && 'Child Development',
        $w('#checkbox14').checked && 'Food',
                $w('#checkbox15').checked && 'Employment',
                $w('#checkbox16').checked && 'Neighbourhoods',
                $w('#checkbox17').checked && 'Volunteering',
        $w('#checkbox18').checked && 'Community Health',
        ].filter(Boolean)

 const query = checkedservice.reduce(
                (query, serviceCategory, i) =>
                i === 0
                ? query.eq('servicecategory', serviceCategory)
                : query.or(wixData.query('programsANDservices').eq('servicecategory', serviceCategory)),
                wixData.query('programsANDservices')
        )

        query.find().then(results => {
 //console.log(results.items);
 let programsANDservices = results.items
                $w('#serviceRepeater').data = programsANDservices
        })
}

$w.onReady(function () {
        ServiceFilter();
});

export function checkbox7_change(event) {
 //Add your code for this event here: 
    ServiceFilter();
}

export function checkbox8_change(event) {
 //Add your code for this event here: 
    ServiceFilter();
}

export function checkbox9_change(event) {
 //Add your code for this event here: 
    ServiceFilter();
}

export function checkbox10_change(event) {
 //Add your code for this event here: 
        ServiceFilter();
}

export function checkbox11_change(event) {
 //Add your code for this event here: 
    ServiceFilter();
}

export function checkbox12_change(event) {
 //Add your code for this event here: 
    ServiceFilter();
}

export function checkbox13_change(event) {
 //Add your code for this event here: 
    ServiceFilter();
}

export function checkbox14_change(event) {
 //Add your code for this event here: 
    ServiceFilter();
}

export function checkbox15_change(event) {
 //Add your code for this event here: 
    ServiceFilter();
}

export function checkbox16_change(event) {
 //Add your code for this event here: 
    ServiceFilter();
}

export function checkbox17_change(event) {
 //Add your code for this event here: 
    ServiceFilter();
}

export function checkbox18_change(event) {
 //Add your code for this event here: 
    ServiceFilter();
}

Hi, Ashley! I think the problem is in this code:

const checkedservice = [
                $w('#checkbox7').checked && 'Health',
                $w('#checkbox8').checked && 'Housing',
                $w('#checkbox9').checked && 'Education',
                $w('#checkbox10').checked && 'Mental Health',
                $w('#checkbox11').checked && 'Parenting',
                $w('#checkbox12').checked && 'Child Care',
                $w('#checkbox13').checked && 'Child Development',
        $w('#checkbox14').checked && 'Food',
                $w('#checkbox15').checked && 'Employment',
                $w('#checkbox16').checked && 'Neighbourhoods',
                $w('#checkbox17').checked && 'Volunteering',
        $w('#checkbox18').checked && 'Community Health',
        ].filter(Boolean)

 const query = checkedservice.reduce(
                (query, serviceCategory, i) =>
                i === 0
                ? query.eq('servicecategory', serviceCategory)
                : query.or(wixData.query('programsANDservices').eq('servicecategory', serviceCategory)),
                wixData.query('programsANDservices')
        )

        query.find().then(results => {
 //console.log(results.items);
 let programsANDservices = results.items
                $w('#serviceRepeater').data = programsANDservices
        });

First of all make sure you’re referencing the value of the checkbox and not just a string: https://www.wix.com/corvid/reference/$w.Checkbox.html#value
Also I don’t think JavaScript reduce function is a right solution for this case.

I would suggest two ways:

  1. The first approach would be getting all the items from the database and then filter them using the regular JavaScript “filter” function: Array.prototype.filter() - JavaScript | MDN

  2. The same approach with wixQuery now, but I don’t think you need JavaScript “reduce” function in this case. Have a look on examples here: https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html

Hi Vikotoriia,

Thanks for your speedy response! Just wondering if I truly need to use the filter function since the first filter is working without issue?

Cheers,
Ashley

It’s just another option.

Also I’d suggest to double-check your fields names, like you write in your query:

.eq(‘servicecategory’, serviceCategory))

your second argument use camelCase and usually, when you add a new field to the database, the field key is generated in camelCase as well, so check if there isn’t a typo in your code