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.
users for more on the fields used in user documents. On the server, this will fetch the record from the database. To improve the latency of a method that uses the user document multiple times, save the returned record to a variable instead of re-calling Meteor. user() .
`accounts-base` This package is the core of Meteor's developer-facing user accounts functionality.
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 : "";
}
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;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With