Judging from this comment by David Glasser in the GitHub issues:
this.userId
is the primary API andMeteor.userId()
is syntactic sugar for users new to JavaScript who might not understand the details of successfully using this yet
It seems like we should use this.userId
whenever possible (such as inside a method function, where you can use both), and only use Meteor.userId()
inside publish functions. If this assumption is correct, why?
(Referring to the relevant bits of the code would also be helpful, I can't seem to find it)
Your question seems to conflate
Meteor.userId()
andMeteor.user()
. The body of the question seems to be asking about the former while the subject line is asking about the latter. I'll try to address both.Meteor.userId()
orMeteor.user()
will cause an error. Instead, usethis.userId
orMeteor.users.findOne(this.userId)
, respectively. However, note that the publish function is only called when a client subscribes. If you want the publication to change when the user record changes, you'll need toobserve()
the cursor returned byMeteor.users.find(this.userId)
and take appropriate action when the record changes.On the server, while a method call is being processed,
Meteor.userId()
andMeteor.user()
will correspond to the ID of the calling user and their record, respectively. However, be aware that calls toMeteor.user()
will result in a DB query because they are essentially equivalent toMeteor.users.findOne(Meteor.userId())
.Directly within a method call, you can also use
this.userId
instead ofMeteor.userId()
, but you are unlikely to see a significant performance difference. When the server receives the method call, it runs your method implementation with the user's ID (and some other info) stored in a particular slot on the fiber.Meteor.userId()
just retrieves the ID from the slot on the current fiber. That should be fast.It's generally easier to refactor code that uses
Meteor.userId()
thanthis.userId
because you can't usethis.userId
outside of the method body (e.g.this
won't have a 'userId' property within a function you call from the method body) and you can't usethis.userId
on the client.Meteor.userId()
andMeteor.user()
will not throw errors andthis.userId
will not work. Calls toMeteor.user()
are essentially equivalent toMeteor.users.findOne(Meteor.userId())
, but since this corresponds to a mini-mongo DB query, performance probably won't be a concern. However, for security reasons the object returned byMeteor.user()
may be incomplete (especially if theautopublish
package is not installed).Simply speaking, Meteor.userId() queries the DB everytime you use it. In client side ( logically ), it looks fine - since we have minimongo.
In server side, using Meteor.userId(), consumes extra resources on SERVER, which, at times is undesired.
Now, this.userId is more over like a session variable m ie it will have a value only when there is a userid attached with the current session. And hence,using 'this' reference wont go and fetch the database everytime, but rather than that it used the active session userId.
Consider performance as a factor. That is the main reason for using this.userId rather than Meteor.userId