Showing error text when user filters with Wix data

Not sure if this has already been asked, but when a user is searching (filtering) a database, if the thing they are looking for does not exist, how do you display text saying that the item is not there?

Also, how do you have one field filter multiple different fields in a dataset (i.e. filtering Name and ID)?

If one/ both of these articles exist already, can someone link them?

For no results check out the length as per query - Velo API Reference - Wix.com

For more info, we are using the import wixData from ‘wix-data’ ;

export function searchBox_keyPress ( event ) {
filter ( $w ( ‘#searchBox’ ). value );
}

function filter ( title ) {
$w ( ‘#dataset1’ ). setFilter ( wixData.filter (). contains ( ‘websiteName’ , title ));
}

formula for our filter engine

function filter(searchvalue) {
    $w('#dataset1').setFilter(wixData.filter().contains('websiteName', searchvalue))
    .then((res)=> {
        console.log("Dataset is now filtered"); 
        console.log("RESULTS: ", res);
    })
    .catch((err)=> {console.log(err);});
}

What do you get in CONSOLE for RESULTS?

I get “Dataset is now filtered RESULTS: undefined” in the console log. Is that what you’re asking about?

I’m sorry for not making it clear, we want text to display on the website so the member knows that the thing they searched for is not in the DB.

Hi there …

After the filter is applied, check the total of items that match the filter criteria by using the getTotalCount( ) function.

const filter = wixData.filter();
$w('#dataset').setFilter(filter).then(() => {
    const matches = $w('#dataset').getTotalCount();
    if (mathces > 0) {
        // Show results
    } else {
        $w('#noResults').text = 'No results match your search criteria.';
    }
})

Hope this helps~!
Ahmad

@russian-dima The setFilter( ) method returns a promise of void, so your " res " will definitely be undefined .

@ahmadnasriya Ok, if i am honest → did not test it xD. So perhaps count it he better way to got. Perhaps i mistakenly mixed it up wix-data, where you can get the lenght of results. → This happens when you do not use datasets often. :sweat_smile: