Reputation: 128
So far I've been looking all over the interwebs and the Meteor docs but nothing has been working for me.
I am creating an account with
Accounts.createUser({
username: username,
email: email,
password: password
});
And I know that is working since {{#if currentUser}}
is working.
However, I am trying to get the current logged-in user's username with something such as
var username = Meteor.userId();
console.log(username);
var username = Meteor.userId().username;
console.log(username);
But neither is working, when I use Meteor.userId()
I just get a random(I'm guessing encrypted) string of numbers and letters, and when I use Meteor.userId().username
it says it's undefined.
All help is welcome and sorry for my possibly terrible grammar, It's very late here!
Upvotes: 9
Views: 31540
Reputation: 12230
All in One!
Meteor.users.findOne({ _id: Meteor.userId() }).username
Then you can either returned value to variable, State or Session ..etc
Upvotes: 1
Reputation: 11
Meteor.userId();
This code above return a string that is the unique ID of the logged user Meteor.userId() not the whole user object. So you can not attempt to any property . To have the current username do as following: 1 - / In meteor controller
var uniqueID = Meteor.userId();
var username = Meteor.users.findOne({_id: uniqueID}).usename;
2 - / In meteor template
{{ currentUser.username }}
Upvotes: 0
Reputation: 21
I am doing it like this:
User needs to be logged in, else Meteor.user() returns null.
Edit: Also keep in mind Meteor.user() will not be available until the users collection is sinced to the client.
Upvotes: 2
Reputation: 21
you have to use username: Meteor.user()
in the .js file
and in the html file use {{username.profile.name}}
Upvotes: 2
Reputation: 11
add to clients js
Accounts.ui.config({ passwordSignupFields: 'USERNAME_AND_EMAIL' });
Upvotes: 1
Reputation: 1282
I think that Accounts.createUser -> username will set up only Meteor.user().profile.name. And if you want to have Meteor.user().username you should probably use also Accounts.onCreateUser ( https://docs.meteor.com/#/full/accounts_oncreateuser ) and add username field like:
Accounts.onCreateUser(function (options, user) {
user.username = user.profile.name;
return user;
});
It is that way because Meteor.user().username is a custom field in this case.
Upvotes: 1
Reputation: 151875
Meteor.userId()
returns the _id of the user from the Mongo.users collection. That's why it looks like a meaningless string.
You want Meteor.user()
. The docs are your best friend :)
Upvotes: 11