(SOLVED) Filter message ''No results found''

Hi!

I need a little help with this code:

import wixData from ‘wix-data’;

$w.onReady(() => {

wixData.query('Type') 

    .find() 

    .then(res => { 

let options = [{ “value”: “”, “label”: “All Categories” }];

        options.push(...res.items.map(type => { 

return { “value”: type.search, “label”: type.search };

        })); 

        $w("#dropdownfilter").options = options; 

    }) 

});

let lastFilterSearch;

let lastFilterType;

let debounceTimer;

export function searchbar_keyPress(event, $w) {

if (debounceTimer) {

    clearTimeout(debounceTimer); 

    debounceTimer = undefined; 

} 

debounceTimer = setTimeout(() => { 

    filter($w("#searchbar").value, lastFilterType); 

}, 200); 

}

function filter(search, type) {

let count = $w(“#dataset2”).getTotalCount();

if (lastFilterSearch !== search || lastFilterType !== type) {

let newFilter = wixData.filter();

if (search)

        newFilter = newFilter.contains('town', search); 

if (type)

        newFilter = newFilter.eq('category', type); 

    $w("#dataset2").setFilter(newFilter); 

    lastFilterSearch = search; 

    lastFilterType = type; 

} 

$w("#dataset2").onReady(() => { 

if (count > 0) {
$w(“#text19”).hide();
$w(“#text19”).collapse();
$w(“#gallery1”).expand();
$w(“#gallery1”).show();
}

if (count === 0) {
$w(“#text19”).show();
$w(“#text19”).expand();
$w(“#gallery1”).collapse();
$w(“#gallery1”).hide();
}

}); 

}

export function dropdownfilter_change(event, $w) {

filter(lastFilterSearch, $w("#dropdownfilter").value); 

}

I’m trying to get the ‘‘NO RESULTS FOUND’’ message when someone does a search and there aren’t any results. With provided code, I am getting that message but with a huge delay when filtered which causes complete unfunctional-wrong search results.

Any help?

Thanks in advice!

To handle cases where there is no data, use a catch [function:](function:
Promise.prototype.catch() - JavaScript | MDN)
[Promise.prototype.catch() - JavaScript | MDN](function:
Promise.prototype.catch() - JavaScript | MDN)

Thanks for your replay! Unfortunately, a catch function doesn’t work.

However, I found a different solution with Search button action/on click and ‘‘No Results Found’’ message works properly with everything else.

Here is the code and hope it will help someone:

import wixData from 'wix-data';

export function button4_click(event) {
    $w("#dataset2").setFilter(wixData.filter()
            .contains("category", $w('#dropdownfilter').value)
            .contains("town", $w('#searchbar').value)

        )
        .then(() => {

 let count = $w("#dataset2").getTotalCount();

 if (count > 0) {
                $w("#text19").hide();
                $w("#text19").collapse();
                $w("#gallery1").expand();
                $w("#gallery1").show();
            }

 // if repeater returns no results, then show message
 if (count === 0) {
                $w("#text19").show();
                $w("#text19").expand();
                $w("#gallery1").collapse();
                $w("#gallery1").hide();
            }
        });

    $w("#dataset2").onReady(() => {

    });

}

Cheers!