【问题标题】:Publishing all Meteor.users doesn't work发布所有 Meteor.users 不起作用
【发布时间】:2015-03-03 00:46:17
【问题描述】:

我正在尝试将所有用户名发布给客户端,即使没有登录。为此,在我拥有的服务器上:

Meteor.publish("users", function() { 
   return Meteor.users.find({}, {fields : { username : 1 } });
});

在客户端:

Meteor.subscribe("users");

但是,当我尝试访问 Meteor.users 集合时,我什么也没找到。

(这与这里的问题基本相同:Listing of all users in the users collection not working first time with meteor js,只是没有先检查管理员的角色。似乎仍然不起作用..)

我可能错过了一些愚蠢的东西..

【问题讨论】:

  • 您尝试在客户端的何处访问此光标?
  • 您确定在访问数据时订阅已准备就绪?
  • @stubailo 这有关系吗?反应式模型不应该在准备好后更新我的数据吗?
  • 那么您在显示所有用户的模板中可能做错了。
  • @PeppeL-G 与模板无关。 count() 返回 0。

标签: meteor


【解决方案1】:

我发现了同样的问题,经过研究我发现this 包,我认为它应该可以帮助你。

看看,希望对你有帮助

更新

首先将订阅移动到/lib文件夹,以确保它在启动时首先做的事情,同时在/lib文件夹上像这样更改订阅。

Tracker.autorun(function() {
if(Meteor.isClient) {
  if (!Meteor.user()) {
    console.log("sorry you need to be logged in to subscribe this collection")
   }else{
    Meteor.subscribe('users');
   }
 } 
});

为了更好的安全性,我们只在客户端登录时订阅用户集合

【讨论】:

  • 如果您认为这是一个错误,请创建一个可重现的项目并create an issue 处理它。
  • 抱歉,我的错误是安装错误是一个“熟悉的问题”
  • 你为什么使用 Meteor.autorun?
【解决方案2】:

此代码将所有用户名输出给客户端,即使未登录(在本例中为 /users 页面):

服务器/publications.js:

Meteor.publish("userlist", function () {
    return Meteor.users.find({},{fields:{username:1}});
});

客户端/users_list.js:

Template.usersList.helpers({
    users: function () {
        return Meteor.users.find();
    }
});

客户端/users_list.html:

<template name="usersList">
      {{#each users}}
        {{username}}
      {{/each}}
</template>

lib/router.js(使用 iron:router 包):

Router.route('/users', {
    name: 'usersList',
    waitOn: function(){
        return Meteor.subscribe("userlist");
    }
});

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 2015-12-11
    • 1970-01-01
    • 2013-03-23
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多