I have a collection and a number of filters. First when I start the page, I want to display the total number of rows of the collection. Although this sounds pretty simple, for some reason my code is not working.
$w.onReady(() => {
let myTest=getNumberOfResults();
console.log(myTest)
$w(‘#text26’).text =getNumberOfResults() //+ " items"
}
function getNumberOfResults () {
wixData.query(“Wedding_Database”)
.find()
.then( (results) => { let resultCount = results.totalCount.toString();
console.log(resultCount) return resultCount
} ) ;
}
Within the function, the console.log(resultCount) works and returns the right value. However the console.log(myTest) gives me undefined. I tried differetn things (.value, etc.) but nothing works. Any idea where the issue might come from?
I did it first without the function, where it worked perfectly. However I need the functions as I want to count the number of results also after I applied some filters and for this I thought having a function is easier than having the code multiple times.
I can’t see the filters in your code, and it depends on how you filter (by direct query? dataset.setFilter() ? running JS code to filter the full results array? ).
@svenstrebel even here it depends on what exactly you want.
You didn’t set query.limit(1000) so by default you get maximum 50 results.
Do you want to see the number of the returned results (max 50) or the number of the total entries in your collection?
@jonatandor35 And how can I change the code so that the function returns the value? In my logic, whatever the console.log() inside the function shows, should also be returned by the function. And I don’t understand why it is not.
On top of that, I want to update the number while filtering. I tried this, which is inside the fiter:
$w(‘#dataset1’).setFilter(newFilter);
//Get number of element let resultCount = $w(‘#dataset1’).setFilter(newFilter).totalCount.toString();
$w(‘#text26’).text = resultCount + " Items"
.totalCount doesn’t work with the filter. I looked at aggregate but from the code it doesn’t look like I could combine the two.
If I have a filter, which works fine, is there a commend to get the number of filtered rows?
I think it’s not a good idea to use direct query (as you posted above) and to use dataset for filtering, because it means you go to the backend one extra time (and you should think about the performance). Why don’t you use dataset only?
as for your question, you should try something like:
I am acutally facing performance issues. The collection has 1001 rows, the repeater shows 12 elements including a picture for each. I have 8 filters and it takes forever to load. If I understand you correctly, you would only use database1 and not Wedding_Database, correct?
Do you have some other tricks how to improve the performance of repeaters?
@svenstrebel I recommended to only use the data set because it’s probably faster than using both and it’s easier for me to explain how to do it.
But it’s not necessarily the best performance. Maybe running direct queries for filtering will be even better.
@svenstrebel first of all, I stopped using datasets a long time ago, so maybe the current performance is good enough (you should ask someone who uses it.
second for filtering by direct quesries you should use:
.eq()
.gt()
.etc…
something like:
@svenstrebel repeaters are slow, That’s a known problem.
You should not assign 1000 items to the repeater together.
There’re some solutions:
divide the results array into groups of 20 (each of the in a different array), assign the first array to the repeater, add “Next” and “Back” buttons, once the user clicks “Next” assign the next array to the repeater.
set an interval and every 2 seconds assign 10 more to the repeater. Something like:
function filter2(myPriceRange) {
wixData.query("Wedding_Database")
.eq("price", myPriceRange).limit(1000).find()
.then( r=> {
let items = r.items;
let newItems = items.splice(0, 10);
let toAssign = [...newItems];
$w('#repeater1').data = toAssign;
let assignData = setIterval(() => {populateRepeater}, 2000);
function populateRepeater(){
if(items.length > 0) {
newItems = items.splice(0, 10);
toAssign.push(newItems);
toAssign = toAssign.flat();
$w('#repeater1').data = toAssign;
} else {clearInterval(assignData);}
}
})
}