发布函数中的 Meteor.userId()

标签 meteor meteor-accounts

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/

相关文章:

javascript - 如何使用 meteor 进行 API 调用

javascript - 使用现有访问 token 使用 Google 的 gapi 库授权客户端 JS API 调用?

javascript - 显示 Meteor 中的所有用户

javascript - 如何使 Disqus 与 url 路由一起使用?

session - 如何在 meteor 中将正则表达式发送到服务器端

Meteor:如何在注册过程中为用户分配不同的角色

javascript - meteor 帐户-ui-bootstrap-3 : Change {{ > loginbuttons }} to icon

javascript - meteor 账号登录失败

javascript - 如何使用熨斗:router to create a separate link for each item in collection

javascript - 数据完全渲染时的 React/Meteor 初始化函数