Mongodb Meteor/Mongo:$无法检索用户组
我试图返回当前用户所属的所有组。每个用户都有一个groups字段,它只是用户所属组的id数组。我的代码是这样的: 我的服务器返回正确组的方法Mongodb Meteor/Mongo:$无法检索用户组,mongodb,meteor,Mongodb,Meteor,我试图返回当前用户所属的所有组。每个用户都有一个groups字段,它只是用户所属组的id数组。我的代码是这样的: 我的服务器返回正确组的方法 userGroups: function(){ var currentUser = Meteor.users.find({_id: Meteor.userId()}); return Groups.find({_id: { $in: currentUser.groups}}); } 然后在my helper中调用该方法: groups:
userGroups: function(){
var currentUser = Meteor.users.find({_id: Meteor.userId()});
return Groups.find({_id: { $in: currentUser.groups}});
}
然后在my helper中调用该方法:
groups: function(){
return Meteor.call('userGroups');
}
我试着在控制台中调试这个,但我只是越来越困惑。我可以调用
var user=Meteor.users.find(\u id:Meteor.userId())
,它会正确地将当前用户分配给变量,但当我调用user.groups
(组id的数组)时,它会说它未定义。如果我在meteor mongo命令行界面中检查文档,则当前用户有一个带有组id的groups字段。meteor中的find query返回一个游标,该游标不是数组,而是对象
您应该添加.fetch()或使用findOne()
这应该管用
发布组表单Meteor.users集合的更新
要将Meteor.users集合中的组添加到acccounts password软件包执行的Meteor.user()自动发布,您需要将其包含到空发布中
大概是这样的:
Meteor.publish(null, function () {
if (!this.userId) return this.ready();
return Meteor.users.find({_id: this.userId}, {
fields: {
profile : 1,
emails : 1,
groups : 1
}
});
});
应该是
.fetch()[0]
仍不工作。我调用该方法的方式有问题,因为即使我更改服务器上的userGroups方法以仅返回所有组返回组。find()
,在groups helper中调用它时也不会返回任何内容。但是,如果我将groups helper更改为return groups.find()
,它确实会返回所有组(这不是我想要的,只是想看看哪些有效,哪些无效)。好的,那么我猜您已经删除了autopublish,并且您的用户集合中有组密钥。如果是这种情况,出于安全原因,组密钥将不会自动发布。我将更新我的答案,关于如何包括groups@Eli,现在看看!这是问题之一。我没有向客户提供groups字段。另一个问题是自定义meteor方法在默认情况下不是被动的。我应该使用内置的publish方法(这是被动的)来只向客户机提供正确的组。一旦我这样做了,所有的问题都解决了。非常感谢你的帮助
Meteor.publish(null, function () {
if (!this.userId) return this.ready();
return Meteor.users.find({_id: this.userId}, {
fields: {
profile : 1,
emails : 1,
groups : 1
}
});
});