Meteor documentation明确指出在发布函数中不允许使用 Meteor.userId()
,并且应该使用 this.userId
来代替它。
我依稀记得过去在不小心执行此操作时看到过警告或错误。
我经常使用共享代码,这些代码既用于验证订阅中的权限,又用于确定帮助程序中是否应该实际向特定用户类型显示特定元素。
这使得这种方法非常烦人。今天我偶然在以下出版物中使用了 Meteor.userId(),没有任何问题。
此限制已被删除吗?如果是这样,是否有任何附带的文档,因为文档尚未更新。
我使用的是 Meteor 1.5.2.2。
示例出版物是这个...
Meteor.publish("users.currentUser", function(){
const currentId = Meteor.userId();
return Meteor.users.find({_id: currentId}, {
fields: { services: 1, username: 1, emails: 1, roles: 1, details: 1}
});
})
最佳答案
自 1.5.1 you can use Meteor.userId()
and Meteor.user()
in publications 。好像文档还没有更新。
关于发布函数中的 Meteor.userId(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46872792/