userId

169 views
Skip to first unread message

Vianney Lecroart

unread,
Nov 30, 2012, 9:54:00 AM11/30/12
to meteo...@googlegroups.com
Hello,

Just a little detail. I don't know if it's only me but I think it's disturbing to see that userId is sometime a value and sometime a function.

In publish this.userId is a value.

In Meteor.methods, this.userId is a value, and you call a function to change it setUserId()

In Meteor.userId(), it's a function that must be called to get the value.

Regads,

Vianney Lecroart

Website | Blog | LinkedIn | Facebook | @acemtp (Twitter)

Theodore Blackman

unread,
Dec 16, 2012, 4:06:19 PM12/16/12
to meteo...@googlegroups.com
It's not just you.  This has caused me at least one somewhat difficult bug, where a line of code like `if (userId === Meteor.userId){...` was trying to tell if a string ID was the same as a function.  Took me a while to understand why it was always returning false ...

I like that `Meteor.userId()` is a function, because it's reactive.  `this.userId` in publish functions is not reactive as far as I can tell, and it really means something different, so it makes sense not to have the exact same API call for both cases.  I think that my confusion stemmed mostly from them both having the same name.  If, for example, one was `Meteor.getUserId()` and the other was this.userId, then I think it wouldn't be as confusing (also it would be more likely to throw errors instead of just silently failing).  Another possibility could be to rename this.userId to something more like this.clientUserId or this.currentUserId to clarify that it's local to that function, not part of the Meteor global.  

matt debergalis

unread,
Dec 16, 2012, 4:11:53 PM12/16/12
to meteo...@googlegroups.com
It's a little weird to me that `Meteor.userId()` doesn't work inside
publish functions. I think there was a reason for this but it escapes
me now.

Publish functions automatically rerun if the user changes. So they're
reactive on user ID, just w/ different mechanics. They are not
reactive wrt the database API.
> --
> You received this message because you are subscribed to the Google Groups
> "meteor-core" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/meteor-core/-/x6FCOHMna1IJ.
>
> To post to this group, send email to meteo...@googlegroups.com.
> To unsubscribe from this group, send email to
> meteor-core...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/meteor-core?hl=en.

Theodore Blackman

unread,
Dec 16, 2012, 4:16:33 PM12/16/12
to meteo...@googlegroups.com
Interesting.  I had always assumed there was some sort of esoteric fiber/threading problem with using Meteor.userId() inside publish.

matt debergalis

unread,
Dec 16, 2012, 4:31:04 PM12/16/12
to meteo...@googlegroups.com
Ah. packages/accounts-base/accounts_server.js:83.

///
/// CURRENT USER
///
Meteor.userId = function () {
// This function only works if called inside a method. In theory, it
// could also be called from publish statements, since they also
// have a userId associated with them. However, given that publish
// functions aren't reactive, using any of the infomation from
// Meteor.user() in a publish function will always use the value
// from when the function first runs. This is likely not what the
// user expects. The way to make this work in a publish is to do
// Meteor.find(this.userId()).observe and recompute when the user
// record changes.
var currentInvocation = Meteor._CurrentInvocation.get();
if (!currentInvocation)
throw new Error("Meteor.userId can only be invoked in method
calls. Use this.userId in publish functions.");
return currentInvocation.userId;
};


On Sun, Dec 16, 2012 at 1:16 PM, Theodore Blackman
Reply all
Reply to author
Forward
0 new messages