流星雨-仅仅公布一个集合的计数

时间:2022-06-22 19:46:26

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 tried but it does not work:

是否可能只向用户发布集合的计数?我想在主页上显示总数,但不把所有的数据传递给用户。这是我试过的,但没有用:

Meteor.publish('task-count', function () {
    return Tasks.find().count();
});

this.route('home', { 
    path: '/',
    waitOn: function () {
        return Meteor.subscribe('task-count');
    }
});

When I try this I get an endless loading animation.

当我尝试这个时,我得到了一个无尽的加载动画。

2 个解决方案

#1


16  

Meteor.publish functions should return cursors, but here you're returning directly a Number which is the total count of documents in your Tasks collection.

流星。发布函数应该返回游标,但是这里直接返回一个数字,这是任务集合中文档的总数。

Counting documents in Meteor is a surprisingly more difficult task than it appears if you want to do it the proper way : using a solution both elegant and effective.

如果你想用正确的方法计算文档,那么在流星中计算文档比看起来要困难得多:使用既优雅又有效的解决方案。

The package ros:publish-counts (a fork of tmeasday:publish-counts) provides accurate counts for small collections (100-1000) or "nearly accurate" counts for larger collections (tens of thousands) using the fastCount option.

package ros:publish计数(tmeasurement day的一个分支:publish计数)为小型收集(100-1000)或使用fastCount选项对大型收集(数万)的“近乎精确”计数提供精确计数。

You can use it this way :

你可以这样使用:

// server-side publish (small collection)
Meteor.publish("tasks-count",function(){
  Counts.publish(this,"tasks-count",Tasks.find());
});

// server-side publish (large collection)
Meteor.publish("tasks-count",function(){
  Counts.publish(this,"tasks-count",Tasks.find(), {fastCount: true});
});

// client-side use
Template.myTemplate.helpers({
  tasksCount:function(){
    return Counts.get("tasks-count");
  }
});

You'll get a client-side reactive count as well as a server-side reasonably performant implementation.

您将得到客户端反应计数以及服务器端合理的性能实现。

This problem is discussed in a (paid) bullet proof Meteor lesson which is a recommended reading : https://bulletproofmeteor.com/

这个问题在一节(付费的)防弹流星课中被讨论,这节课推荐阅读:https://hbr.org/2014/04/04/08

#2


6  

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.

注意,此解决方案不会反应。然而,如果需要反应性,它可以被合并。

#1


16  

Meteor.publish functions should return cursors, but here you're returning directly a Number which is the total count of documents in your Tasks collection.

流星。发布函数应该返回游标,但是这里直接返回一个数字,这是任务集合中文档的总数。

Counting documents in Meteor is a surprisingly more difficult task than it appears if you want to do it the proper way : using a solution both elegant and effective.

如果你想用正确的方法计算文档,那么在流星中计算文档比看起来要困难得多:使用既优雅又有效的解决方案。

The package ros:publish-counts (a fork of tmeasday:publish-counts) provides accurate counts for small collections (100-1000) or "nearly accurate" counts for larger collections (tens of thousands) using the fastCount option.

package ros:publish计数(tmeasurement day的一个分支:publish计数)为小型收集(100-1000)或使用fastCount选项对大型收集(数万)的“近乎精确”计数提供精确计数。

You can use it this way :

你可以这样使用:

// server-side publish (small collection)
Meteor.publish("tasks-count",function(){
  Counts.publish(this,"tasks-count",Tasks.find());
});

// server-side publish (large collection)
Meteor.publish("tasks-count",function(){
  Counts.publish(this,"tasks-count",Tasks.find(), {fastCount: true});
});

// client-side use
Template.myTemplate.helpers({
  tasksCount:function(){
    return Counts.get("tasks-count");
  }
});

You'll get a client-side reactive count as well as a server-side reasonably performant implementation.

您将得到客户端反应计数以及服务器端合理的性能实现。

This problem is discussed in a (paid) bullet proof Meteor lesson which is a recommended reading : https://bulletproofmeteor.com/

这个问题在一节(付费的)防弹流星课中被讨论,这节课推荐阅读:https://hbr.org/2014/04/04/08

#2


6  

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.

注意,此解决方案不会反应。然而,如果需要反应性,它可以被合并。