-1

I have a collection that I publish to client, limiting number of documents and sending ones that matter to client. Like this:

Meteor.publish('list', function() {
  return someList.find( { matters: true }, { limit: 50 } );
});

But I also want to send to clients some other values from that collection, like total count of documents, average value of some field, etc.

How to make server to calculate it and send value to client how will just display it?

1
  • Thanks for downvote without any suggestion and explanation. How awesome community!
    – 10robinho
    Aug 3, 2015 at 21:56

2 Answers 2

1

For simple counts, you can use the publish-counts package.

For something more advanced (like an average) could:

  1. Use an observer - see my answer to this question.
  2. Write the statistics back into a collection and publish that. A good way to accomplish that is with collection-hooks. Essentially, after every update you could modify a document containing the averages and other stats.
  3. Write a method to compute the values and return them to the client. This has the advantage of being the simplest solution, but it lacks reactivity.
1

Personally I use a global variable, and return it in a method.

collec_count = 0;

Meteor.publish('list', function() {
    var coll = someList.find( { matters: true }, { limit: 50 } );
    collec_count = coll.count();
    return coll;
});

Meteor.methods({
findDB_count: function () {
    return collec_count;
},

Then on the client side I just call the method inside an helper/event:

Meteor.call('findDB_count', function (error, result) {
    if (error) {
        alert(error.reason);
    } else {
        Session.set("count", result);
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.