Hi,
I am confronted with a small problem …
I make an application that contains products …
On a page, I want to display my products by 20 with a paging system. I got my data from a publication I subscribe to … with option to define the number of elements to skip according to the page number and got only specifics fields…
So far, so good.
Now I have to retrieve the total number of products from this same page to display a counter like: Page 1 (20 products on 50000).
of course, I do not want to load the entire collection just to know the number of items in my collection …
So, I tried to make a second publication to only count the number of elements in the collection, but it is apparently not the right way to do, since I have no way to access this value since the Variable of my collection.
I currently have not found a way to assign the content of a publication using the same collection than another to a different variable name than the one of the collection … Moreover, a publication seem to be supposed to return only a array or object . But here I need only a simple number. The publication does not seem to be the right method to use to obtain this value.
So… I was going to recover this value with a “call” method.
It works, I get my value, but I can not use it in a helper probably because the request is asynchronous.
At first I tried to run the method on the onCreated event of my template and assign it to my template Template.myTemplate.productsCount = …
Then return this value from my helper … But it does not work.
I also tried to make the call from the Tracker.autorun function from the helper … But without more success. I can not return the value recover by calling the method, however I can display it with a console.log …
gotProductsCount(){
Tracker.autorun((computation) => {
Meteor.call("productsCountInACategory", this.id, function (err, value) {
if (err) {
alert('error')
}
console.log(value); // Yes it show my value in my console...
return value
});
});
}
I try also…
gotProductsCount(){
function Return(val){
console.log(val); // Yes it show the val in my console...
return val
}
Tracker.autorun((computation) => {
Meteor.call("productsCountInACategory", this.id, function (err, value) {
if (err) {
alert('error')
}
console.log(value);
Return(value)
});
});
}
So, my question is which will be the best way to got different data from the same Collection, from the same page, but without load the entire data ?
And how to use a async methods inside a helper ?
Thank’s for your help,
Mickael