【问题标题】:Meteor.userId vs Meteor.userId()Meteor.userId 与 Meteor.userId()
【发布时间】:2014-08-10 19:50:53
【问题描述】:

我有一小段代码来更新我的用户配置文件中的名称:

Meteor.users.update({_id: Meteor.userId()}, {$set:{"profile.name": name}});

当我在本地工作时,我可以毫无问题地使用Meteor.userIdMeteor.userId()。但是,当我部署到 Modulus 时,我遇到了问题。如果我没有操作员,它将执行初始 $set,但不再执行。如果我使用操作符,它的行为与我预期的一样。

这是为什么?我认为我不应该在没有操作员的情况下使用它,但是它有什么原因可以工作吗?

【问题讨论】:

  • Meteor.userId() 和 Meteor.userId 不能相同。 Meteor.userId 返回一个函数 Meteor.userId() 返回一个字符串。我认为您在谈论 this.userId,它仅在出版物和 Meteor.methods 中可用。
  • 不,这可能很奇怪,我在服务器端使用 Meteor.userId,而不是在出版物中。
  • 可能您将 _id 设置为 Meteor.userId 函数的字符串表示形式。像"function() { ... }" 这样的东西。这将成功设置值,但这不是您想要的。您能否打开您的 mongo 控制台并查看一些文档以查看 _id 字段的样子? meteor mongo 来自 CL
  • @pahan,为了清楚起见,你写的 Meteor.userId 返回一个函数,这是不正确的。函数返回值(可能),而Meteor.userId 存储(是)一个函数。

标签: meteor userid


【解决方案1】:

看看documentation

  • 函数Meteor.userId() 可用“除发布函数外的任何地方”

  • 变量this.userId 可在“任何地方”使用(服务器端发布功能也明确调用)。

【讨论】:

    【解决方案2】:

    在尝试使用 mocha 进行单元测试时,我遇到了与 Meteor.userId() 相同的问题。

    一个简单的解决方法是转到 tasks.js 并将 Meteor.userId() 替换为 this.userId 是使用函数的this 上下文。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 2017-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多