Hi there,
I just finished the awesome guide on meteortips.com, but one thing left me a bit puzzled.
In one of his chapters, David says we need to use Meteor.userId() on the client side, and on the server side this.userId. However, in the next chapter he uses himself Meteor.userId() on the server. I tried it and it works.
Which leads me to the question:
What exactly is the difference between the two, and when should they be used?
Calling Meteor.userId() from a publish function will throw an exception, so there is no “insecure” way.
As for the reason of this design choice, I only have a vague idea so I will let other explain :- )
Yes it is. I think it’s not a design choice either.
It may be something for backward compatibility. You should try to use this.userId for most of the times.
Try to avoid called Meteor.user() on the server. It’ll fetch the user object from Mongo, everytime you invoke it.
Do not use this approach if you just want to check that User is logged in. On the Server Meteor.user() reads user data from Meteor.users collection. And note that this.userId inside Meteor.methods could be overriden during method call (see this.setUserId)! So it is safely to use Meteor.userId. But inside Meteor.publish handlers only this.userId is available and immutable.