[英]How to get current user's username with Meteor
So far I've been looking all over the interwebs and the Meteor docs but nothing has been working for me. 到目前为止,我一直在寻找各种各样的互联网和Meteor文档,但没有任何东西对我有用。
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. 而且我知道自
{{#if currentUser}}
正在运行以来它正在发挥作用。
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. 但是当我使用
Meteor.userId()
时,两者都没有工作,我只是随机(我猜是加密的)数字和字母串,当我使用Meteor.userId().username
它说它是未定义的。
All help is welcome and sorry for my possibly terrible grammar, It's very late here! 所有的帮助都是受欢迎的,对不起我可能很糟糕的语法,这里已经很晚了!
Meteor.userId()
returns the _id of the user from the Mongo.users collection. Meteor.userId()
从Mongo.users集合返回用户的_id。 That's why it looks like a meaningless string. 这就是为什么它看起来像一个毫无意义的字符串。
You want Meteor.user()
. 你想要
Meteor.user()
。 The docs are your best friend :) 文档是你最好的朋友:)
试试{{currentUser.username}}
你必须在.js文件和html文件中使用username: Meteor.user()
使用{{username.profile.name}}
I think that Accounts.createUser -> username will set up only Meteor.user().profile.name. 我认为Accounts.createUser - > username只会设置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:
如果你想拥有Meteor.user()。用户名,你应该使用Accounts.onCreateUser( https://docs.meteor.com/#/full/accounts_oncreateuser )并添加用户名字段,如:
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. 这是因为Meteor.user()。用户名是这种情况下的自定义字段。
add to clients js 添加到客户端js
Accounts.ui.config({ passwordSignupFields: 'USERNAME_AND_EMAIL' });
I am doing it like this: 我是这样做的:
User needs to be logged in, else Meteor.user() returns null. 用户需要登录,否则Meteor.user()返回null。
Edit: Also keep in mind Meteor.user() will not be available until the users collection is sinced to the client. 编辑:还要记住,在将用户集合发送到客户端之前,Meteor.user()将不可用。
All in One! 一体!
Meteor.users.findOne({ _id: Meteor.userId() }).username
Then you can either returned value to variable, State or Session ..etc 然后,您可以将值返回到变量,State或Session ..etc
Meteor.userId();
This code above return a string that is the unique ID of the logged user Meteor.userId() not the whole user object. 上面的代码返回一个字符串,该字符串是已记录用户Meteor.userId()的唯一ID,而不是整个用户对象。 So you can not attempt to any property .
所以你不能尝试任何财产。 To have the current username do as following: 1 - / In meteor controller
要使当前用户名执行如下操作:1 - /在流星控制器中
var uniqueID = Meteor.userId();
var username = Meteor.users.findOne({_id: uniqueID}).usename;
2 - / In meteor template 2 - /在流星模板中
{{ currentUser.username }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.