guillaumek guillaumek - 1 year ago 94
Javascript Question

Meteor: Count the number of collection item in relational collection

I'm doing a lists/tasks application, I can select the list who display me the good tasks using a session to store which list I'm clicking.
Using the Meteor task board tutorial, I can count how items I have.
the problem is the count system is not related to all lists but to only the list I'm.

I can't understand how to count the items for each lists, and display the counter in front of each lists item.
in the tutorial, the counter is in Session, so do I have to create a new "counter" in the collection, or can I keep the session ?

Some code:

if (Meteor.isServer) {
Meteor.publish('lists', function listsPublication() {
return Lists.find({
$or: [
{private: { $ne: true}},
{owner: this.userId},


incompleteCount() {
return Tasks.find({ listId: Session.get('listId'), checked: { $ne: true } }).count();
'click .list-selected' (event) {

var list = $(event.currentTarget).attr('list-id');
Session.set('listId', list);


tasks() {
const instance = Template.instance();

if (instance.state.get('hideCompleted')) {
return Tasks.find({
listId: Session.get('listId'),
checked: { $ne: true },
}, { sort: Session.get("sort_order") });
return Tasks.find({listId: Session.get('listId')}, { sort: Session.get("sort_order")});

lists() {
return Lists.find({}, { sort: { createdAt: -1 } });


Thanks in advance

Answer Source

Thanks @CodeChimp, I could find the way just using a param in my helper:

    incompleteCount(listId) {
        return Tasks.find({ listId: listId, checked: { $ne: true } }).count();

And the template (using jade):

        span {{incompleteCount this._id}}

Using the search I did before in one list, but I just added the param instead of the session.get() who just get the active list ...


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download