Reputation:
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.
Upvotes: 7
Views: 9837
Reputation: 1404
This has happened to me, too, using loginWithFacebook. I use this function, which has worked without problems so far:
var reallyLoggedIn = function() {
var user = Meteor.user();
if (!user) return false;
else if (!user.profile) return false;
else return true;
};
Upvotes: 1
Reputation: 1712
I am not able to reproduce this problem, but if you have the UserID, you can get all the information from the full database, Meteor.users (even though it SHOULD be doing this already).
Template.login.user_name = function() {
return (Meteor.userId() ? Meteor.users.findOne({_id:Meteor.userId()}).profile.name : '')
}
Upvotes: 1
Reputation: 12231
It's possible that the data has not yet arrived from the server. Instead of just checking for Meteor.userId, what happens if you check for the property?
Template.login.user_name = function() {
return Meteor.userId() && Meteor.user() && Meteor.user().profile ? Meteor.user().profile.name : "";
}
Upvotes: 10