After logging a user in with Meteor.loginWithPassword()
or creating a new one with Accounts.createUser
(both client-side), I can confirm in their callbacks that Meteor.user()
indeed contains all the set record's properties.
{ _id: "XXX",
profile: {
name: "Joe Shmoe",
thumbnail: "http://www.YYY.com/ZZZ.jpg"
},
username: "joeshmoe" }
Furthermore, according to the official docs,
By default, the current user's username, emails and profile are published to the client.
So, would anyone be able to tell why when I try to access these fields in my Templates thusly
Template.login.user_name = function () {
return (Meteor.userId() ? Meteor.user().profile.name : '')
};
it fails due to Meteor.user()
only returning {_id: "XXX"}
with none of its actual properties? I.e. the user is definitely logged in, but the user object suddenly lost/is hiding all of its properties.
Anyone know what the problem might be?
Many thanks.
EDIT: this happens with Meteor 0.5.4, the latest version at this time of writing. The accepted answer indeed fixes the issue; sometimes Meteor.userId()
is already valid before the rest of the Object has arrived from the server. Thanks everyone.
// Server Meteor.publish("userData", function() { return Meteor.users.find({_id: this.userId}, {fields: {profile: 1, username: 1}}); }); ... // Client Meteor.autosubscribe(function () { Meteor.subscribe("userData"); });
// Client Meteor.methods({ userName: function() { return Meteor.user().profile.name; }); ... Template.login.user_name = function () { return (Meteor.userId() ? Meteor.call("userName") : '') };