Javascript 如何使用 Meteor 获取当前用户的用户名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26728155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:17:16  来源:igfitidea点击:

How to get current user's username with Meteor

javascriptmeteor

提问by David Chalifoux

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().usernameit says it's undefined.

但是两者都不起作用,当我使用时,Meteor.userId()我只会得到一个随机(我猜是加密的)数字和字母字符串,当我使用Meteor.userId().username它时,它说它是未定义的。

All help is welcome and sorry for my possibly terrible grammar, It's verylate here!

所有帮助是值得欢迎和我可能是可怕的语法对不起,这是非常晚了!

回答by Dan Dascalescu

Meteor.userId()returns the _id of the user from the Mongo.userscollection. That's why it looks like a meaningless string.

Meteor.userId()Mongo.users集合中返回用户的 _id 。这就是为什么它看起来像一个毫无意义的字符串。

You want Meteor.user(). The docsare your best friend :)

你要Meteor.user()。该文档是你最好的朋友:)

回答by darkcode

Try with {{currentUser.username}}

试试 {{currentUser.username}}

回答by Nikhil

you have to use username: Meteor.user()in the .js file and in the html file use {{username.profile.name}}

你必须username: Meteor.user()在 .js 文件和 html 文件中使用{{username.profile.name}}

回答by Angel Pateiro

I am doing it like this:

我这样做:

  1. Include an accounts package: meteor add accounts-password
  2. import { Meteor } from 'meteor/meteor'
  3. console.log('Username: ' + Meteor.user().username);
  1. 包含一个账户包:meteor add accounts-password
  2. 从'流星/流星'导入{流星}
  3. console.log('用户名:' + Meteor.user().username);

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() 将不可用,直到用户集合被发送到客户端。

回答by Deano

All in One!

一体!

 Meteor.users.findOne({ _id: Meteor.userId() }).username

Then you can either returned value to variable, State or Session ..etc

然后您可以将值返回给变量、状态或会话 .. 等

回答by sgp

add to clients js

添加到客户端js

Accounts.ui.config({
  passwordSignupFields: 'USERNAME_AND_EMAIL'
});

回答by juliancwirko

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.createUser -> username 只会设置 Meteor.user().profile.name。如果你想要 Meteor.user().username 你也应该使用 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().username 在这种情况下是一个自定义字段。

回答by payene

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

上面的代码返回一个字符串,它是登录用户 Meteor.userId() 的唯一 ID,而不是整个用户对象。所以你不能尝试任何属性。要使用当前用户名,请执行以下操作:1 - / 在流星控制器中

var uniqueID = Meteor.userId(); 
var username = Meteor.users.findOne({_id: uniqueID}).usename;

2 - / In meteor template

2 - / 在流星模板中

{{ currentUser.username }}