Hi everyone,
I hold three boolean fields ‘paper’, ‘ebook’ and ‘audio’, on a database ‘chapterpages’ and need to use them as filters in a search. The user will click a checkbox (#checkPaper, #checkEbook or #checkaudio) and this needs to be ‘true’ on the database.
Here is the functioning code for the existing search with the new code indicated. The new code doesn’t even fail the search and trigger the error message!
Help much appreciated.
function search2() {
$w( ‘#searchFail’ ).hide();
wixData.query( ‘chapterpages’ )
.eq( ‘bookType’ , $w( “#dropdownType” ).value)
.and ((wixData.query( ‘chapterpages’ )
.contains( ‘genre1’ , $w( “#dropdownGenre” ).value))
.or (wixData.query( ‘chapterpages’ )
.contains( ‘genre2’ , $w( “#dropdownGenre” ).value)))
//new code
.and ((wixData.query( ‘chapterpages’ )
.contains( ‘paper’ , $w( “#checkPaper” ).checked))
.or (wixData.query( ‘chapterpages’ )
.contains( ‘ebook’ , $w( “#checkEbook” ).checked))
.or (wixData.query( ‘chapterpages’ )
.contains( ‘audio’ , $w( “#checkAudio” ).checked)))
//new code ends
.find()
.then(res => {
if ( res.items.length > 0 )
{$w( ‘#listRepeater’ ).data = res.items}
else {
$w( ‘#searchFail’ ).show()
$w( ‘#listRepeater’ ).data = res.items}
})
}
It would be probably better, if you would first show a part of your DATABASE-STRUCTURE.
‘paper’, ‘ebook’ and ‘audio’ are just three boolean type fields on the database ‘chapterpages’. They are input from checkboxes and appear as little checks on the database when ‘true’. That’s about it.
Edit your Check-Box IDs like following:
- “checkpaper”
- “checkebook”
- “checkaudio”
Then this code should work for you…
import wixData from 'wix-data';
$w.onReady(()=> {
$w('Checkbox').onChange((event)=>{
let elementID = event.target.id
console.log(elementID)
// if(elementID==="checkpaper") {start_FilterEngine("paper")}
// if(elementID==="checkebook") {start_FilterEngine("ebook")}
// if(elementID==="checkaudio") {start_FilterEngine("audio")}
start_FilterEngine(elementID)
})
});
function start_FilterEngine(FIELD) {
wixData.query("chapterpages")
.contains(FIELD, $w(FIELD).checked)
.find()
.then( (results) => {console.log(results)
if(results.items.length > 0) {
let firstItem = results.items[0];
console.log(firstItem)
}
else { }
})
.catch( (err) => {let errorMsg = err;});
}
EDIT: Little FIX!!!
You will have to modify this part of CODE here…
.contains(FIELD, $w(FIELD).checked)
especialy…
.contains(FIELD,.....)
You will have to convert for example —> checkpaper —> to —> paper
Perhaps by using —> substring() 
Thanks for that, russian-dima, but I’ve been wrestling with this all day and the problem sees to lie not in my code, but in the fact that in the search:
.and ((wixData.query( ‘chapterpages’ )
.eq( ‘paper’ , $w( “#checkPaper” ).checked))
.or (wixData.query( ‘chapterpages’ )
.eq( ‘ebook’ , $w( “#checkEbook” ).checked))
.or (wixData.query( ‘chapterpages’ )
.eq( ‘audio’ , $w( “#checkAudio” ).checked)))
the ‘paper’ ‘ebook’ and ‘audio’ database fields are accepting ANY value as being equal to them, whether ‘true’, ‘false’ or even ‘xxxx’. The only time they take it as not equal is if the input field is ‘undefined’ - in other words it has never been given a value.
Not sure where to proceed from here.
Correction: when the database field is ‘undefined’, not the input field.
Does anyone know whether this is a known problem, or am I overlooking something obvious? I’m reluctant to go down the inelegant path of reproducing the boolean fields on the database as ordinary text fields so I can use in the search.
Take a look here…
https://russian-dima.wixsite.com/meinewebsite/boolean-data
It is the same example with some fixes…
import wixData from 'wix-data';
$w.onReady(()=> {
$w('Checkbox').onChange((event)=>{
let elementID = event.target.id
console.log(elementID)
start_FilterEngine(elementID)
})
});
function start_FilterEngine(VALUE) {
let FIELD = VALUE.substring(5,VALUE.length)
let STATE = $w('#'+VALUE).checked
console.log(FIELD)
console.log(STATE)
wixData.query("chapterpages")
.eq(FIELD, STATE)
.find()
.then( (results) => {console.log(results)
if(results.items.length > 0) {
let firstItem = results.items[0];
console.log("RESULTS = ", results.items)
}
else { }
})
.catch( (err) => {let errorMsg = err;});
}
Take also a look onto CONSOLE.
Thanks for that, but it’s the solution to a problem I don’t have.
I check all three fields on each search and if one of them matches a ‘true’ field on the database, the record should be accepted. The problem is that a match is shown, whether the database or input fields are true, false or anything else. I’ve checked this rigorously with console.log (as you suggest) and I can’t find a way around it.
I’ve now taken the messy solution of creating ordinary text fields on the database in parallel to the boolean ones. When I check these it works fine - so nothing wrong with the logic. A satisfactory result, though not aesthetically pleasing!
If I understand your problem (and your code) correctly, you need to use .eq() for a boolean filter. As shown in the API documentation:
.contains() only allows string value:

.eq() allows any value type:

Thanks. I did change my code to .eq, but it didn’t seem to make a difference.
I suspect that you have other issues with your filter. You should instead of starting out with a complex filter, try a very simple example that just uses those three boolean fields. Once you see the query properly working, you can add the other filter conditions.
Good advice, I’ll look further into it.
Or, like already shown in my working example 
All you have to do, is to MODIFY it to your own needs 
Did you test my example? Did you look onto CONSOLE?
Did you see the results?
Change the behaviour of CODE to your own needs.
Many thanks to both of you. I’ve got it working - though as I said, not in the most direct and satisfactory way. When I have time I’ll investigate further.