【问题标题】:Publishing Meteor Users to Admin将 Meteor 用户发布到管理员
【发布时间】:2013-08-15 21:30:48
【问题描述】:

我正在尝试将所有电子邮件和“角色”发布给“管理员”用户(使用 meteor-roles 陨石包),服务器知道它试图发布什么,但由于某种原因客户端没有接听数据。这是涉及的一段代码:

服务器代码:

Meteor.publish("directory", function() {
   if(Roles.userIsInRole(this.userId, 'admin')) {
      //next line shows that it outputs that there are 2 documents in this
      console.log(Meteor.users.find({}, {fields:{emails:1, roles:1}}).count());
      return Meteor.users.find({}, {fields:{emails:1, roles:1}});
   } else {
      return {};
   }
}

客户代码:

Meteor.subscribe("directory");
Directory = new Meteor.Collection("directory");

//and then to simulate my use for this collection
console.log(Directory.find().count());
//which outputs 0

为什么客户没有得到文件? (而且我肯定使用角色为“admin”的帐户登录)

谢谢!

编辑和解决方案!

好的,所以我知道我做错了什么。我只需要在服务器上发布“目录”,并在客户端订阅。然后,所有用户数据都进入 Meteor.users 集合,我不应该定义自己的“Directory=new Meteor.Collection('directory')”。然后我可以通过 Meteor.users 访问数据。干杯!

服务器:使用与上面相同的代码

客户:

Meteor.subscribe("directory");
console.log(Meteor.users.find().count()); //outputs 2, yay!

【问题讨论】:

    标签: meteor meteorite


    【解决方案1】:

    您的收藏可能应该在一开始就定义好,以便客户知道在哪里写!

    此外,您的Meteor.subscribe("directory"); 在应用加载时运行一次。你应该让它反应。

    Directory = new Meteor.Collection("directory");
    
    Deps.autorun(function(){
      Meteor.subscribe("directory");
    });
    

    【讨论】:

    • 这还不够;依赖函数还必须以某种方式触摸 userId 以向 Meteor 指示它应该在依赖更改时重新运行。它可以像 function() {if Meteor.userId() {Meteor.subscribe('directory');}} 一样简单。
    • 由于某种原因,这些都不起作用。客户端上的目录仍然包含 0 个文档...我的其他 2 个集合工作正常,只是这个,因为(我认为)它涉及用户。我是否也必须在服务器上声明集合“目录”或在某处更改某些权限?即使我打开不安全和自动发布,它仍然在目录中显示 0 个文档
    • 哦,所以你的服务器上没有这个集合? publish("directory") 创建数据通道,而不是集合 - 从 Meteor.users 集合发布的数据在客户端的同一个集合中找到 - 所以也只需使用 Meteor.users.find(),而不是 Directory.find()。你有理由希望这个系列与众不同吗?
    猜你喜欢
    • 2014-01-06
    • 2016-04-06
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    相关资源
    最近更新 更多