将Meteor用户发布到管理员
我正在尝试将所有电子邮件和“角色”发布给“管理员”用户(使用meteor roles meteorite软件包),服务器知道它要发布什么,但由于某些原因,客户端无法获取数据。下面是涉及到的一段代码: 服务器代码:将Meteor用户发布到管理员,meteor,meteorite,Meteor,Meteorite,我正在尝试将所有电子邮件和“角色”发布给“管理员”用户(使用meteor roles meteorite软件包),服务器知道它要发布什么,但由于某些原因,客户端无法获取数据。下面是涉及到的一段代码: 服务器代码: Meteor.publish("directory", function() { if(Roles.userIsInRole(this.userId, 'admin')) { //next line shows that it outputs that there a
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=newMeteor.collection('Directory')”。然后我可以通过Meteor.users访问数据。干杯
服务器:使用与上面相同的代码
客户:
Meteor.subscribe("directory");
console.log(Meteor.users.find().count()); //outputs 2, yay!
您的集合可能应该在一开始就定义好,以便客户知道在哪里写 另外,您的
Meteor.subscribe(“目录”)代码>在加载应用程序时运行一次。你应该让它反应
Directory = new Meteor.Collection("directory");
Deps.autorun(function(){
Meteor.subscribe("directory");
});
这是不够的;依赖项函数还必须以某种方式触摸userId,以向Meteor指示当依赖项发生变化时应该重新运行它。它可以像函数()一样简单{if Meteor.userId(){Meteor.subscribe('directory');}}。出于某种原因,这两个函数都不起作用。客户端上的目录仍包含0个文档。。。我的另外两个收藏很好用,就是这一个,因为(我认为)它涉及到用户。我是否也必须在服务器上声明集合“目录”,或者在某个地方更改某些权限?即使我打开了“不安全”和“自动发布”,它仍会在DirectoryOh中显示0个文档,所以服务器上没有此集合publish(“目录”)
创建数据通道,而不是集合-从Meteor发布的数据。在客户端的同一集合中可以找到用户集合-因此也只需使用Meteor.users.find()
,而不是directory.find()
。你想让这个系列与众不同,有什么原因吗?