In one of my Meteor.publish()
functions, this.userId
has the value of undefined
. I can't call Meteor.userId()
because it's not available inside a publish function. How are you supposed to get userId
now?

- 16,205
- 16
- 71
- 126

- 177
- 1
- 9
3 Answers
There are four possibilities:
There is no user logged in.
You're calling the method from the server, and there will thus be no user associated with the call (unless you're calling it from another function which will have a user bound to its environment, like another method or a subscribe function).
You don't even have the accounts-base package (or any of the add-ons) installed. I'm only including this for completeness.
You are using an arrow function in ES6.
Meteor.publish('invoices', function() { return invoices.find({by: this.userId}); });
will work just fine, whilstMeteor.publish('invoices', () => { return invoices.find({by: this.userId}); });
will return an empty cursor, becausethis
will have nouserId
property. This happens because an arrow function does not bind its ownthis
,arguments
,super
, ornew.target
.
If it's definitely not (2), what happens when you log Meteor.userId()
immediately before you make the method call on the client?

- 18,934
- 9
- 36
- 50

- 7,993
- 1
- 23
- 29
-
Yeah, it was 2. I had set `var = this.userId` just above `Meteor.publish`, so it was being called from the server. Moving that inside `Meteor.publish` fixed it. Thanks! – Alex Forsyth Jan 16 '15 at 19:45
-
17Also, for the sake of completeness, make sure you are not using arrow function, i.e. `Meteor.publish('invoices', function() { return invoices.find({by: this.userId}); });` will work just fine, whilst `Meteor.publish('invoices', () => { return invoices.find({by: this.userId}); });` will return empty cursor, because this will have no userId. Because an arrow function "does not bind its own this, arguments, super, or new.target". – Ilya Saunkin Jan 12 '16 at 16:43
-
6@ElijahSaounkine Thank you! Bit by the ES6. – joshperry Feb 08 '16 at 03:54
-
oh got...for me it was number 4. I am using Typescript (pbastowski:typescript) and I used the fat arrow in my publish function. Strangely...it worked most of the times but sometimes after reload or recompile the subscription would return nothing. Then I installed another package (composite packages) and after that it didn't work at all. Always undefined (the userID). Even after removing the package, which doesn't make sense, it still didn't work. Then I found your article and you seem to have saved my day. Weird that it used to work with fat arrow (like just 15 minutes before this post..) – Mattijs Feb 09 '16 at 23:40
-
4It was the fat arrow. Thanks! – Fotis Adamakis Jun 23 '16 at 18:48
-
1Cheers this had been driving me mad, it was 4 for me. – Tom Maton Aug 29 '16 at 11:55
-
I had a similar issue, I was using arrow function, that was the problem. Many thanks. – vantesllar Oct 30 '16 at 20:01
-
For folks where the prob is on using the Arrow function. https://guide.meteor.com/data-loading.html#publications has a note that the current api doesn't allow for this.userId. There's a conversation about the details here https://github.com/meteor/meteor/issues/6780 I imagine these days the arrow issue is going to become more and more common. – Joachim Feb 17 '17 at 19:25
-
Spent hours and it's just because of that arrow function! – JohnnyQ Mar 21 '17 at 13:13
FIXED:
import { Meteor } from 'meteor/meteor';
import { Roles } from 'meteor/alanning:roles';
import _ from 'lodash';
import { check } from 'meteor/check';
import Corporations from '../corporations';
Meteor.publish('corporations.list', () => {
const self = this.Meteor; // <-- see here
const userId = self.userId();
const user = self.user();
let filters = {};
if (user) {
if (!Roles.userIsInRole(userId, ['SuperAdminHolos'])) { // No Está en el Rol SuperAdminHolos
filters = { adminsEmails: { $in: _.map(user.emails, 'address') } };
}
return Corporations.find(filters);
} else return;
});
You should use Meteor.userId() instead.

- 6,079
- 5
- 39
- 55
-
It says "Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions." – Alex Forsyth Jan 16 '15 at 19:15
-
Meteor.publish("my_channel", function() { var userId = this.userId; myFunction(userId); }); – Ajay Takur Jan 16 '15 at 19:18
-
-
1`Meteor.publish()` does not have access to `Meteor.userId()`: http://docs.meteor.com/#/full/meteor_userid "Anywhere but publish function" – joshperry Feb 08 '16 at 03:53