【问题标题】:Get single value from meteor server to client从流星服务器获取单个值到客户端
【发布时间】:2015-04-18 15:24:54
【问题描述】:

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

在服务器端我会这样做:

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

用户集合未发布到客户端。如何从服务器返回一个简单的真/假到路由定义?我试过 Meteor.call('hasUser' function(result){ 控制台.log(结果); });

但由于客户端上的调用是异步的,我的结果中总是会出现“未定义”

【问题讨论】:

    标签: javascript meteor iron-router


    【解决方案1】:

    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>
    

    【讨论】:

    • 大卫也许我只是弄错了这个问题,但他不想确保用户有一个现有的帐户来重定向到index 路由?
    • 我已经重读了几次,现在我不确定要问什么。我将添加评论以进行澄清。
    猜你喜欢
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 2020-08-11
    • 2014-09-13
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 2014-03-17
    相关资源
    最近更新 更多