我正在尝试根据用户的角色有选择地发布用户数据。我希望公开电子邮件地址和用户名。如果用户是管理员,他们应该能够看到整个用户个人资料。
在服务器上(CS):
Meteor.publish "allUsers", ->
if Roles.userIsInRole @userId, 'admin' then data = profile: 1
else data = 'profile.name': 1
data['emails.address'] = 1
Meteor.users.find {}, fields: data
在客户端(CS):
Meteor.subscribe("allUsers")
这在大多数情况下都非常有效。当用户注销,然后再次登录时,订阅和
Meteor.user().profile
什么都不返回。刷新页面,profile 字段重新填写。为了尝试解决这个问题,我将服务器返回语句更改为:
[
Meteor.users.find {}, fields: data
Meteor.users.find @userId
]
这什么也没做。这是 Meteor 的错误还是我从根本上误解了登录/注销?
更新
我读了更多,似乎 Meteor 会随机选择数据用于同一集合上的多个订阅/发布。考虑到这一点,我将返回查询更改为:
Meteor.users.find _id: $ne: @userId, fields: data
其他用户的数据暂未公布! (控制台中的 Meteor.users.find().count() 返回 1。)
最佳答案
我相信您的原始返回声明没有问题。但是,我会看看当 @userId
为 null
时,您的 Roles.userIsInRole
函数会发生什么情况。可能那里有导致发布功能崩溃的错误。
我不是 Coffeescripter,但如果这是问题所在,那么我认为将其添加到发布函数的顶部可以解决您的问题:
if not @userId return
关于coffeescript - Meteor 发布附加用户信息登录/注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15901612/