Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does Meteor.userId() throw an error when used in a publication? #2734

Closed
rijk opened this issue Oct 1, 2014 · 3 comments
Closed

Why does Meteor.userId() throw an error when used in a publication? #2734

rijk opened this issue Oct 1, 2014 · 3 comments

Comments

@rijk
Copy link

rijk commented Oct 1, 2014

Due to the following code, Meteor.userId() will throw the following error when used inside a publication function:

Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.

Is there a good reason for breaking the consistency of this API? I understand you can use this.userId in your code, but in packages this is not possible. Would it be possible to store the userId for the current subscription somewhere in the core, and access that in Meteor.userId()?

@Tarang
Copy link
Contributor

Tarang commented Oct 2, 2014

@rjk This has always been the case. Meteor.userId() & Meteor.user() dont work in publish functions. As far as I can remember it has been this way since the accounts system was introduced around 0.5.x.

I suspect its different because of the way reactivity works in publish functions. They're not technically reactive and will re-run regardless of whether you use Meteor.userId() or not- in contrast to the Tracker.autorun/Deps.autorun/Computation methods. I suspect the difference is to ensure people keep this in mind when architecting their publish methods.

@rijk
Copy link
Author

rijk commented Oct 2, 2014

I know, I wasn't reporting it as a bug or anything — I'm just curious if there is a good reason it is like this. Because it would be a lot easier for package developers (and less confusing for everybody) if Meteor.userId() just worked.

Regarding reactivity; publish functions will not rerun indeed, but this doesn't have much to do with Meteor.userId(). Sure this will cause a slight inconstancy in its functionality, but it is the same with database cursors (only the cursor is updated, not the code using it) so I think most devs are aware of this already. By the way, are the publish functions not rerun automatically from the client already when switching users?

@glasser
Copy link
Contributor

glasser commented Oct 13, 2014

This might be awkward, but it is working as intended and we are trying to keep our GitHub issues limited to actual bugs.

One way to think about it is that this.userId is the primary API and Meteor.userId() is syntactic sugar for users new to JavaScript who might not understand the details of successfully using this yet; we expect people to write their first method before they write their first complex publish function.

@glasser glasser closed this as completed Oct 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants