Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor.user() returns only its _id

Tags:

meteor

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.

like image 307
cneuro Avatar asked Jan 09 '13 18:01

cneuro


People also ask

What is the meteor user () function for?

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() .

What is the name of the package that provides basic user accounts functionality?

`accounts-base` This package is the core of Meteor's developer-facing user accounts functionality.


2 Answers

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 : "";
}
like image 105
Rahul Avatar answered Oct 20 '22 00:10

Rahul


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;
};
like image 38
zorlak Avatar answered Oct 19 '22 23:10

zorlak