I would say there are always tons of different ways of how to solve an issue in JS.
What i would do in your case, would be to search for alternatives.
Your own version do not work?
A suggested version do not work either?
Ok, then try to go another way, but how?
For example, would it fit your needs to implement an SWITCH-BUTTON, which would do the → OR-FUNCTION <— in your case?
If so, try this one, using an additional switch-button in your seach, which would decide in which of your queries to be searched…
import wixData from 'wix-data';
$w.onReady(function () {
$w('#btnStartSearch').onClick(()=>{
if($w('#switch1').checked) {filter_referencedContent()}
else {filter_mainContent()}
});
});
function filter_referencedContent() {
wixData.query("Clients")
.contains("clientName",$w('#searchBar').value)
.find()
.then((results)=>{
if(results.items.length>0){
letfirstItem=results.items[0];
filter1=firstItem._id;
$w("#dynamicDataset").setFilter(wixData.filter()
.eq("clientName",filter1));
}
else {}
});
}
function filter_mainContent() {
wixData.query("Companies")
.contains("companyName",$w('#searchBar').value)
.find()
.then((results)=>{
if(results.items.length>0){
letfirstItem=results.items[0];
filter2=firstItem.companyName;
$w("#dynamicDataset").setFilter(wixData.filter()
.eq("companyName",filter2));
}
else {}
});
}
Just an quick not tested example.
And you can even improve it a lot.
When you take a look onto the 2x codes, you will recognize that both are almost identical. You could make it even more dynamic. You have just to recognize what changes (what is different between the 2) and combine the 2 into one single dynamic one.
The marked code-parts differs, all the rest of code, is identical.
Turning a normal function into a → RETURNING ← one would do the trick and you would be able to make just ONE function out of TWO identical.
There are surely also other ways of how to solve your problem!