-->

Meteor.userId vs Meteor.userId()

2019-04-15 04:12发布

问题:

I have a short piece of code, like so, to update the name in my user's profile:

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

When I'm working locally, I can use Meteor.userId or Meteor.userId() without issue. However, when I deploy to Modulus, I run into issues. If I don't have the operator on it, it will do the initial $set, but no more. If I user the operators, it behaves as I would expect.

Why is this? I assume that I shouldn't have been using this without the operator to begin with, but is there a reason why it worked at all?

回答1:

Have a look in the documentation

  • The function Meteor.userId() is available "Anywhere but publish functions"

  • The variable this.userId is available "Anywhere" (which explicitly is also called out for the Server side Publish function).



回答2:

I have had the same issue with Meteor.userId() when trying to get a unit testing with mocha to work.

A simple fix is to go to tasks.js and replace Meteor.userId() with this.userId which is to use the this context of the function.



标签: meteor userid