Meteor - Publish just the count for a collection

后端 未结 3 2061
太阳男子
太阳男子 2021-02-20 00:52

Is it possible to publish just the count for a collection to the user? I want to display the total count on the homepage, but not pass all the data to the user. This is what I t

3条回答
  •  鱼传尺愫
    2021-02-20 01:45

    I would use a Meteor.call

    Client:

     var count; /// Global Client Variable
    
     Meteor.startup(function () {
        Meteor.call("count", function (error, result) {
          count = result;
        })
     });
    

    return count in some helper

    Server:

    Meteor.methods({
       count: function () {
         return Tasks.find().count();
       }
    })
    

    *Note this solution would not be reactive. However if reactivity is desired it can be incorporated.

提交回复
热议问题