Issues with code/ Show "No Results" message after no results from query

Hi, I need help!

I’ve been working on this code for days. I’ve tried a million ways to show a “No Results” message if query comes back = 0. I did it in a million different ways and still the “No Results” text box doesn’t show up. Can someone help verifying my code and see what am I doing wrong? Thanks!

import wixData from ‘wix-data’ ;
$w ( ‘#resetButton’ ). hide ();

$w . onReady ( function () {

});

export function searchButton_click ( event ) {
search ();
}

function search ( ) {

wixData . query ( "Courses1" ) 
. contains ( "order" , String ( $w ( '#dropdown1' ). value )) 

. and ( wixData . query ( “Courses1” )). contains ( “location” , String ( $w ( ‘#dropdown2’ ). value ))
. find ()
. then ( results => {
$w ( ‘#listRepeater’ ). data = results . items ;
});

$w ( '#resetButton' ). show (); 
$w ( '#resetButton' ). enable (); 
$w ( '#searchButton' ). hide (); 

}

export function resetButton_click ( event ) {
$w ( ‘#dynamicDataset’ ). setFilter ( wixData . filter ());
$w ( ‘#dropdown1’ ). value = undefined ;
$w ( ‘#dropdown2’ ). value = undefined ;
$w ( ‘#searchButton’ ). show ();
$w ( ‘#searchButton’ ). enable ();
$w ( ‘#resetButton’ ). hide ();
}
export function searchbutton_click ( event ){
search ();
}
function find ( ) {

wixData . query ( “Courses1” )
. contains ( “order” , String ( $w ( “#dropdown1” ). value ))
. and ( wixData . query ( “Courses1” )
. contains ( “location” , String ( $w ( “#dropdown2” ). value )))
. find () // Run the query
. then ( results => {
if ( results . items . length > 0 ) {
$w ( ‘#listRepeater’ ). data = results . items ;

} else {
$w ( “#listRepeater” ). hide ();
$w ( ‘#noresultsText’ ). show ();
console . log ( “No Results” )
}
})
}

What is returning in your results.items? If it is never 0, see what is returning so you know what to evaluate on.

Hi Amanda, thank you for your response. I’m not a developer. I’m just writing code in from what I see online and finding the logic around it.

I thought “else” would mean that if the query returns with nothing than it would show message. Am I missing something?

Should I instead of putting the “else” code then put

If (results.items.length === 0) {
$w ( “#listRepeater” ). hide ();
$w ( ‘#noresultsText’ ). show ();
console . log ( “No Results” )}})}

?

Because I tried and it didn’t work.

You are not wrong in your approach, but perhaps the results items is never returning a length of zero so the way to know that is to log the results to see what’s there.

after your .then line you can add a console statement to see what is returning

console.log(results.items)