Database "OR" search

Hi,
My goal is to accept either Numbers or Text in the input field “#searchBar”. If can get a single wixData query to work with the Numbers (sNumber), or have two wixData queries (joined by .OR) with text (Sphone and SCounty). But all three together “stall” and no data is retrieved. If, in the Query I substitute “Word2” with actual values, numerical and text, it does work.
My website is https://jeffhanner10.wixsite/rcshelters and the page that the code is from is “test page 3 search”. Any thoughts on what I might be missing?

import wixData from ‘wix-data’;

let Word2

$w.onReady( function () {
search()

});

export function searchButton_click(event) {
Word2 = $w(“#searchBar”).value
$w(“#searchBar”).placeholder = Word2;
search();
}

function search() {
wixData.query(“Shelter4”)
.eq(“sNumber”, Word2)
.or(
wixData.query(“Shelter4”)
.contains(“sCounty”, Word2))
.or(
wixData.query(“Shelter4”)
.contains(“sPhone”,Word2))

.find() 
.then( (results) => { 
    console.log(results.items); 
  $w('#table1').rows = results.items; 

})}

The variable Word2 might not contain a value. Keep in mind that the search bar (text input) may not be updated when the searchButton_click() function is called. You need to give the text input component some time .

Thank you again for your assistance. I added the setTimeout function and it didn’t make any difference. Like I said, the sCounty and the sPhone work, but the sNumber doesn’t. With console.log I could see that the value of Word2 was correct. If I “comment out” the queries for sCounty and sPhone, sNumber works. Do I perhaps have to convert the number to a string first?

Another question. In the pseudo code if variable is a number then … how would I determine variable is a number or text?

I learned that Word2 is being passed to the function search() but in the first query (with sNumber), even if I comment out the other two queries, it won’t work. However, if I substitute a number for Word2 in the first query then all of the queries work together to get the right data. So I don’t understand why Word2 won’t work when the field is a Number. Here is the search () that works.

function search() {
console.log(Word2)
wixData.query(“Shelter4”)
.eq(“sNumber”,77398)
.or(
wixData.query(“Shelter4”)
.contains(“sCounty”, Word2))
.or(
wixData.query(“Shelter4”)
.contains(“sPhone”,Word2))

.find() 
.then( (results) => { 
    console.log(results.items); 
  $w('#table1').rows = results.items; 

})}

@jeffhanner10 sNumber field is apparently a number field. You can try converting Word2 to a number for the filter:

.eq(“sNumber”, parseInt(Word2))
or
.eq(“sNumber”,Number(Word2))

@yisrael-wix Thanks. Both of those worked. Now I move on!!!

1 Like