【问题标题】:How to get current user's username with Meteor如何使用 Meteor 获取当前用户的用户名
【发布时间】:2017-07-23 07:41:56
【问题描述】:

到目前为止,我一直在寻找所有的互联网和 Meteor 文档,但没有什么对我有用。

我正在创建一个帐户

Accounts.createUser({
    username: username,
    email: email,
    password: password
});

而且我知道这是因为 {{#if currentUser}} 正在工作。

但是,我正在尝试使用诸如

之类的内容获取当前登录用户的用户名
var username = Meteor.userId();
console.log(username);

var username = Meteor.userId().username;
console.log(username);

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

欢迎所有帮助,并为我可能糟糕的语法感到抱歉,这里非常晚了!

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

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

    你想要Meteor.user()docs 是你最好的朋友 :)

    【讨论】:

    • 对不起,我忘了说我已经试过了。它以未定义的形式返回。
    • 澄清:仅“Meteor.user()”返回对象 {_id:“2zWQoT7wWWhnXEWjY”,电子邮件:Array[1]}。但是“Meteor.user().username”返回未定义。
    • 您的登录提供商是否设置了用户名? accounts-password 没有,Twitter 有。
    • 我在 OP 中提到了我用于用户名的内容。 “Accounts.createUser”有一个我使用的用户名选项。
    • 对我有用。作为console.log(Meteor.userId(), Meteor.user()); 输出wN9vCjAAQ6S9YB7Ad undefined
    【解决方案2】:

    试试{{currentUser.username}}

    【讨论】:

    • 我已使用此代码,我的用户名现在正在显示。但是当我使用下面的代码时,它没有显示电子邮件地址。那是 {{currentUser.email}} 我是通过用户名登录的,而不是用户名显示的电子邮件,但没有显示电子邮件。
    • @Pritesh 与他人分享代码。他们帮助了你
    【解决方案3】:

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

    【讨论】:

    • 如果您使用 OAuth(例如 Facebook 或 Google 登录),这是正确的版本!请参阅the docs 了解更多信息。
    【解决方案4】:

    我是这样做的:

    1. 包含一个帐户包:meteor add accounts-password
    2. 从“流星/流星”导入 { Meteor }
    3. console.log('用户名:' + Meteor.user().username);

    用户需要登录,否则 Meteor.user() 返回 null。

    编辑:还要记住 Meteor.user() 在用户集合被提供给客户端之前将不可用。

    【讨论】:

      【解决方案5】:

      我认为 Accounts.createUser -> 用户名只会设置 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;
      
      });
      

      之所以这样,是因为 Meteor.user().username 在这种情况下是一个自定义字段。

      【讨论】:

        【解决方案6】:

        添加到客户端 js

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

        【讨论】:

        • 谢谢,非常有帮助。这就是我一直在寻找的。​​span>
        【解决方案7】:

        一体化!

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

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

        【讨论】:

          【解决方案8】:
          Meteor.userId();
          

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

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

          2 - / 在流星模板中

          {{ currentUser.username }}
          

          【讨论】:

            猜你喜欢
            • 2014-08-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-10-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多