Reputation: 339
I have a collection Groups with a filed that is an array of user emails. I need to access the email address of the logged in user in a publish function. The following works:
Meteor.publish("groups", function() {
return Groups.find({emails: "[email protected]"});
});
Which is quite useless obviously. None of these work:
Meteor.user().emails[0].address
this.user.emails[0].address
this.userId.emails[0].address
What's the correct way to access the user email address here?
Upvotes: 1
Views: 1193
Reputation: 19544
Did you put the subscribe
function in a reactive context? The publish function may get invoked first when the user is not yet set, in which case this.user
will be null
. You should check this in your publish method. Also, according to the documentation, there's just this.userId
parameter available, so you need to fetch the user object yourself:
Meteor.publish('groups', function() {
if(!this.userId) return [];
var user = Meteor.users.findOne(this.userId);
... /* use user.emails[0].address to search for and return the right groups */
});
Also, wrap your subscribe function in something reactive:
Deps.autorun(function() {
Meteor.subscribe('groups');
});
Upvotes: 5