从流星服务器获取单个值到客户端

Get single value from meteor server to client

我有一个 setup 路线,第一个用户在其中创建他的帐户。如果他已经有一个帐户,则必须将他重定向到 index 路由。

在服务器端我会这样做:

if(Meteor.users.find().count > 0){
  //do stuff
}

用户集合未发布到客户端。我如何 return 一个简单的 true/false 从服务器到路由定义?我尝试使用 Meteor.call('hasUser' function(result){ console.log(结果); });

但是由于客户端上的调用是异步的,所以我的结果总是 'undefined'

这已经被询问和回答here, however you could also do this with the publish-counts包:

$ meteor add tmeasday:publish-counts

服务器

Meteor.publish('userCount', function() {
  Counts.publish(this, 'userCount', Meteor.users.find());
});

客户

Meteor.subscribe('userCount');
...
Counts.get('userCount');

此外,它还带有以下帮助程序:

<p>There are {{getPublishedCount 'userCount'}} users</p>