【问题标题】:Meteor - missing username Meteor.users.findOne(); or Meteor.users();Meteor - 缺少用户名 Meteor.users.findOne();或 Meteor.users();
【发布时间】:2015-05-03 16:24:03
【问题描述】:

提交帖子后,当前用户似乎无法识别,并且用户名显示为空白。提交的 html 有{{author}} 来显示写帖子的用户。

当我在控制台中输入以下内容时,结果如下:

1)user.username

=> 结果user 未定义。

2)Meteor.users.find();

=> LocalCollection.Cursor {collection: LocalCollection, sorter: null, _selectorId: undefined, matcher: Minimongo.Matcher, skip: undefined…}

3) Meteor.users.findOne();

=> Object {_id: "P3ocCTTdvi2o3JApf", profile: Object}

与工作版本相比(注意我的版本缺少用户名)

=> Object {_id: "XHXPebzjg5LM5tNAu", profile: Object, username: "Bruno"}

在 post.js 集合中(在 lib 文件夹中 - 与客户端和服务器共享),我有:

Meteor.methods({
  postInsert: function(postAttributes) {
    check(this.userId, String);           //check(Meteor.userId(), String);
    check(postAttributes, {
      title: String,
      message: String
    });
    var user = Meteor.user();
    var post = _.extend(postAttributes, {
      userId: user._id, 
      author: user.username
    });
    var postId = Posts.insert(post);
    return {_id: postId};
  },

Discover Meteor 教程中也没有其他对 author 的引用,而且它确实有效。但是我的不起作用。这个问题似乎是在我添加 UserAccounts 包之后开始的。或者可能是文件夹位置问题?

更新 [2015 年 5 月 11 日]

我意识到,当使用 Meteor 附带的原始 ui 帐户时,它具有 username 字段,因为 {{> login}} 附带的注册链接使用用户名 + 密码。不涉及电子邮件地址。

另一方面,UserAccounts 没有此用户名字段。它可以是电子邮件/密码或社交登录。所以也许有人指导我如何从电子邮件/密码和社交网络按钮登录/登录中获取用户名(作为单独的字段或从电子邮件派生)作为开始?然后我会试着从那里摆弄。

用户帐户代码

router.js

//Iron router plugin to ensure user is signed in
AccountsTemplates.configureRoute('ensureSignedIn', {
  template: 'atTemplate',     //template shown if user is not signed in
  layoutTemplate: 'atLayout'  //template for login, registration, etc
});

Router.plugin('ensureSignedIn', { //Don't require user logged in for these routes
  except: ['login', 'register']   //can use only: too 
});

AccountsTemplates.configureRoute('signIn', {  //login
  name: 'login',
  path: '/login',
  template: 'atTemplate',
  layoutTemplate: 'atLayout',
  redirect: '/'
});

AccountsTemplates.configureRoute('signUp', {  //registration
  name: 'register',
  path: '/register',
  template: 'atTemplate',
  layoutTemplate: 'atLayout',
  redirect: '/'
});

并在 config.js 下(服务器端)

【问题讨论】:

  • 不要包含图片,请尝试包含代码(可能还有用于响应的 cmets)。当您直接在控制台中执行 {{expr}} 时,花括号无法进行对象文字解释,因此改为代码块。这意味着它几乎与 expr 完全相同。即您尝试在 Console 中访问的 author 使用的是普通 JavaScript 而不是流星。
  • @PaulS。我的错。让我删除{{expr}} 上的部分。至于代码,由于 devtool 没有抛出错误,我不确定要考虑粘贴代码的哪一部分。我对所有具有{{author}}user 的部分进行了搜索,因此只粘贴了集合中的部分。您对从哪里开始寻找或使用其他工具等有什么建议吗?

标签: javascript meteor


【解决方案1】:

从 Useraccounts 文档中,试试这个:

if (Meteor.isServer){
    Meteor.methods({
        "userExists": function(username){
            return !!Meteor.users.findOne({username: username});
        },
    });
}

AccountsTemplates.addField({
    _id: 'username',
    type: 'text',
    required: true,
    func: function(value){
        if (Meteor.isClient) {
            console.log("Validating username...");
            var self = this;
            Meteor.call("userExists", value, function(err, userExists){
                if (!userExists)
                    self.setSuccess();
                else
                    self.setError(userExists);
                self.setValidating(false);
            });
            return;
        }
        // Server
        return Meteor.call("userExists", value);
    },
});

这会将用户名字段添加到您的登录/注册表单中,并在注册新用户时检查用户名冲突。

顶部是服务器代码,底部是公共代码,因此您可以将整个 sn-p 放在 project/lib/ 文件夹中,或者将方法与其余方法一起放置,具体取决于您如何构建项目.

使用以下命令让用户名字段首先显示在注册表单上,而不是最后显示:

var pwd = AccountsTemplates.removeField('password');
AccountsTemplates.removeField('email');
AccountsTemplates.addFields([
  {
    _id: "username",
    type: "text",
    displayName: "username",
    required: true,
    func: function(value){
      if (Meteor.isClient) {
        console.log("Validating username...");
        var self = this;
        Meteor.call("userExists", value, function(err, userExists){
          if (!userExists)
            self.setSuccess();
          else
            self.setError(userExists);
          self.setValidating(false);
        });
        return;
      }
      // Server
      return Meteor.call("userExists", value);
    },
    minLength: 5,
  },
  {
    _id: 'email',
    type: 'email',
    required: true,
    displayName: "email",
    re: /.+@(.+){2,}\.(.+){2,}/,
    errStr: 'Invalid email',
  },
  pwd
]);

https://github.com/meteor-useraccounts/core/blob/master/Guide.md

【讨论】:

    【解决方案2】:

    您需要使用.s 来访问对象的属性,而不是空格

    Meteor.users.findOne
    

    【讨论】:

    • 抱歉错误,您在. 位置是对的,如上所述修改了我的帖子。有什么建议我应该在代码中查找问题吗?
    【解决方案3】:

    您提供的信息很少,无法发现您的问题。 这就是说,你是如何让你的用户进入的?

    请注意,OAuth 注册不会在创建的用户对象上提供username 字段,并且仅基于电子邮件地址进行密码注册。在这些情况下,user.username 将是 undefined

    我建议确保您的所有用户都获得profile.username(可能会利用一些Accounts.onLogin 挂钩或某些个人资料页面来强制用户选择用户名。

    在此之后,您将扩展您的帖子元数据设置author: user.profile.username

    【讨论】:

    • 我正在使用 UserAccounts :) 。以前,ui-accounts 会直接连接。这是否意味着 UserAccounts 需要手动连接?
    • 好吧,useraccounts 所做的不过是Accounts 包提供的功能。它只是调用Accounts.loginWith<Sevice> 和类似的,就是这样。我不确定accounts-ui 是否会在基础包中添加更多功能:我会说不!
    • @splendiido ui-accounts 版本不需要 Accounts.loginWith<Service> 或任何 profile.username 并且登录工作正常。所以我不确定需要为 UserAccounts 插入什么。
    • 您能否提供更详细的说明,说明您正在做什么以及您想要实现的目标?
    • 感谢@splendido - 我正在尝试实现UserAccounts。我意识到我可能错过了用户名字段,这解释了为什么原始 ui-accounts 版本有 username 字段而我使用 UserAccounts 没有。我需要了解如何在登录/注册中包含该字段或解决上述问题的任何方法。没有其他错误消息,所以我自己不确定在这里还要写什么
    猜你喜欢
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    相关资源
    最近更新 更多