Prevent search button from showing entire database results when text input is left blank

Hi Guys

So I have text input connected to my database, this text input in turn is connected to a search button (see code below). Where the search button is BUTTON1 and the text input is #INPUT1 and data is then displayed in a table on the same page #TABLE1

export function button1_click(event) {
wixData.query(‘RegisteredMicrochips’)
.contains(‘microchipNumber’, $w(‘#input1’).value)
.find()
.then(res => {
$w(‘#table1’).rows = res.items;
});

This all works fine, if a user enters a 15 digit number which corresponds to a matching value
under the MICROCHIPNUMBER field in the database, the table will display the corresponding info. What is ALSO happening though (which I want to prevent) is that if NO value is added into the text input, and the search button is clicked. THE ENTIRE database is populated in the table. ie: ALL MICROCHIPNUMBER fields and their corresponding data are populated in the table.

How do I prevent this from happening as this would be a privacy breach.

Why are you using .contains() and not .eq() ?
According to your description you want a full match. Am I wrong?

ok, awesome. Just gave that a try and its working! THANK YOU!. While I have your attention, would you mind if I query you about another issue I’m having which is actually preventing me from putting my site live?

You could add a check. I apologize ahead of time because I’m on my phone and using the wix app so it wont look pretty and Wont be 100% what you would type in.

But something along the lines of

If ($w(“#input1”).value !== “”) {
Run query
}

Or you could use length !== 0

And possibly .value !== null or undefined.

Basically if the value is not empty, run the query.

Hi Patrick, thanks for the tip! J.D’s advice of changing the .contains() to .eq() solved the problem

Awesome! Since J.D’s comment was the solution, mark it as top comment so others can see what the solution was. :+1:

@patrick done.