Need database query to run again if initial search return no results

Hi guys.

I have been working at this all day and still cannot get it to work.

I am querying a database that has item codes in it.

The user will enter an item code and the data query will return some product information on that item code.

I need a query to run again with all lower case characters if the initial query containing upper case characters return no values.

Can you help?

Below is what I have.

import wixData from "wix-data";

export function articleInput_input(event) {

wixData.query("DB1FKCompetitor") 
 .eq("articleCode", $w("#articleInput").value)
 //used to filter the results by price
 .find()  // Run the query
  .then(res => {   
 if(res.items.length > 0) {
 let item = res.items[0];
 $w("#table1").rows = res.items; 
  }
else {    
//$w("#articleInput").value = $w("#articleInput").value.toLowerCase()
wixData.query("DB1FKCompetitor") 
 .eq("articleCode", $w("#articleInput").value.toLowerCase())
.find()  // Run the query
  .then(res2 => {  
$w("#table1").rows = res2.items; 
  })
}
})}

$w.onReady(function () {
 $w("#table1").columns = [{
 "id": "1",
 "dataPath": "fkMatch",
 "label": "fkMatch",
 "type": "string",
 "width": 100,
 }];
});

Alan, the query code itself looks to be fine. The problem lies with making too many calls to the server for data because there is always going to be a little lag time for the data to be returned, resulting in inconsistent results. You can put a console.log(res) line after the .then to see what I’m talking about.

The solution is to not fire the query code after every character/number is input. Putting the code in a button click function is a common way this is done. It’s not like you need to search after each character is entered, but only after they have entered the entire item code.

Thanks Anthony.

Will give it a try.

I was not sure if I coded the below part correctly to convert the input to all lowercase if it fails the first time.

Does it look OK to you?

.eq("articleCode", $w("#articleInput").value.toLowerCase())

It does.