Hi, I’m trying to apply filters to a query not using function chaining, but it’s not working. According to the documentation the “query” and “eq” methods return a WixDataQuery. So I would like your help in uderstanding why the correct execution only works with chaining function, like the example below.
This way it WORKS:
let q = wixData.query('vehicles').eq('brand','TRIUMPH').eq('category','motorcycle');
q.find().then((r) => {
console.info(r.totalCount);
});
Now, I need to apply filters according to user choices and that’s why I don’t want to use chaining, the way shown below DOESN’T work, the result was not filtered:
let q = wixData.query('vehicles');
q.eq('brand','TRIUMPH');
q.eq('category','motorcycle');
q.find().then((r) => {
console.info(r.totalCount);
});
Can anybody help me understand why?
What about something in this way?
import wixData from 'wix-data';
$w.onReady(function () {start_Filter()})
function start_Filter() {
let statusQuery = wixData.query("myCollection")
.eq("status", "pending")
.or(wixData.query("myCollection")
.eq("status", "rejected")
);
let ageQuery = wixData.query("myCollection")
.lt("age", 25)
.or(wixData.query("myCollection")
.gt("age", 65)
);
statusQuery.and(ageQuery)
.find()
.then( (results) => {
if(results.items.length > 0) {
let items = results.items;
let firstItem = items[0];
let totalCount = results.totalCount;
let pageSize = results.pageSize;
let currentPage = results.currentPage;
let totalPages = results.totalPages;
let hasNext = results.hasNext();
let hasPrev = results.hasPrev();
let length = results.length;
let query = results.query;
} else {
// handle case where no matching items found
}
})
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
});
}
Modify it for your own needs.
And this one…
let q = wixData.query('vehicles');
q.eq('brand','TRIUMPH');
q.eq('category','motorcycle');
q.find().then((r) => {
console.info(r.totalCount);
});
Should perhaps be more like…
let q = wixData.query('vehicles')
.eq('brand','TRIUMPH')
.eq('category','motorcycle')
.find().then((r) => {
console.info(r.totalCount);
});
Let me know if it works for you with this modification?
import wixData from 'wix-data';
$w.onReady(function () {start_Filter()})
function start_Filter() {
let statusQuery = wixData.query("myCollection");
statusQuery.eq("status", "pending");
statusQuery.or(wixData.query("myCollection")
.eq("status", "rejected")
);
let ageQuery = wixData.query("myCollection");
ageQuery.lt("age", 25);
ageQuery.or(wixData.query("myCollection")
.gt("age", 65)
);
statusQuery.and(ageQuery)
.find()
.then( (results) => {
if(results.items.length > 0) {
let items = results.items;
let firstItem = items[0];
let totalCount = results.totalCount;
let pageSize = results.pageSize;
let currentPage = results.currentPage;
let totalPages = results.totalPages;
let hasNext = results.hasNext();
let hasPrev = results.hasPrev();
let length = results.length;
let query = results.query;
} else {
// handle case where no matching items found
}
})
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
});
}
@paullera
What do you mean?
Which modifications? What did you change?
@russian-dima this:
let statusQuery = wixData.query("myCollection")
.eq("status", "pending")
.or(wixData.query("myCollection")
.eq("status", "rejected")
);
to this:
let statusQuery = wixData.query("myCollection");
statusQuery.eq("status", "pending");
statusQuery.or(wixData.query("myCollection")
.eq("status", "rejected")
);
I’ve found where I went wrong, it was lack of attention…
If you don’t use function chaining your objective must get the result of the function call.
Wrong way:
let q = wixData.query('vehicles');
q.eq('brand','TRIUMPH');
q.eq('category','motorcycle');
q.find().then((r) => {
console.info(r.totalCount);
});
Right way:
let q = wixData.query('vehicles');
q = q.eq('brand','TRIUMPH');
q = q.eq('category','motorcycle');
q.find().then((r) => {
console.info(r.totalCount);
});
Sorry about the confusion.
@Paulo Willers
No problem, i just wanted to to offer another solution… (using dataset)…
function start_Filter (parameter) {
let filter = wixData.filter()
let myKategory = $w('#dropdown1').value
let myTutorial = $w('#dropdown2').value
if ($w('#dropdown1').value!=0) {filter = filter.contains('reference1', myKategory);}
if ($w('#dropdown2').value!=0) {filter = filter.eq('reference2', myTutorial);}
$w('#dataset1').setFilter(filter)
}
https://russian-dima.wixsite.com/meinewebsite/blank-3