我有一个setup 路线,第一个用户在其中创建他的帐户。如果他已经有一个帐户,他必须被重定向到 index 路由。
在服务器端我会这样做:
if(Meteor.users.find().count > 0){
//do stuff
}
用户集合未发布到客户端。我怎样才能从服务器返回一个简单的 true/false 到路由定义?我尝试使用 Meteor.call('hasUser' function(result){ 控制台日志(结果); });
但由于调用在客户端是异步的,所以我的结果总是“未定义”
最佳答案
这已经被询问和回答了here , 但是您也可以使用 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>
关于javascript - 从 meteor 服务器获取单个值到客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28569860/