7

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.

2
  • I even tried to publish them on the server and autosubscribe them on the client, but no change. I shouldn't need to do that though. // Server Meteor.publish("userData", function() { return Meteor.users.find({_id: this.userId}, {fields: {profile: 1, username: 1}}); }); ... // Client Meteor.autosubscribe(function () { Meteor.subscribe("userData"); });
    – cneuro
    Jan 9, 2013 at 18:24
  • Then I put the the whole call into a getter method into Meteor.methods, suspecting the Templates context to disallow certain DB accessors for security, but it's exactly the same problem. // Client Meteor.methods({ userName: function() { return Meteor.user().profile.name; }); ... Template.login.user_name = function () { return (Meteor.userId() ? Meteor.call("userName") : '') };
    – cneuro
    Jan 9, 2013 at 18:25

3 Answers 3

11

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 : "";
}
1
  • Indeed, that seems to have been the problem. I kinda suspected it was something benign, I just didn't make the cognitive leap to explicitly check the rest of the object as well, since I trusted Meteor.userId() to either be null or valid along with the rest of the user data. Thanks for that.
    – cneuro
    Jan 10, 2013 at 16:46
2

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;
};
1

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 : '')
}
1
  • Yes, I had tried that too but this call was also returning undefined, as if the user had been there on creation and then simply disappeared. This could be explained by the fact that the client simulates the correct response (one of the many things I adore about Meteor), then the server rectifies it immediately, not having written the record yet, and is only valid again on subsequent, insistent tries.
    – cneuro
    Jan 10, 2013 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.