簡體   English   中英

流星-缺少用戶名Meteor.users.findOne(); 或Meteor.users();

[英]Meteor - missing username Meteor.users.findOne(); or Meteor.users();

提交帖子后,當前用戶似乎無法被識別,用戶名顯示為空白。 提交的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};
  },

在“發現流星”教程中也沒有其他對author引用,並且可以正常工作。 但是我的行不通。 添加UserAccounts程序包后,似乎已經開始出現此問題。 還是文件夾位置問題?

更新[2015年5月11日]

我意識到使用流星隨附的原始ui帳戶時,它具有username段,因為{{> login}}隨附的注冊鏈接使用用戶名+密碼。 沒有涉及的電子郵件地址。

另一方面,UserAccounts沒有此用戶名字段。 它是電子郵件/密碼或社交登錄。 因此,也許有人指導我如何從電子郵件/密碼和社交網絡按鈕登錄/登錄中獲取用戶名(作為獨立字段或從電子郵件派生)? 然后生病從那里弄弄。

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下(服務器端)

在“用戶帳戶”文檔中,嘗試以下操作:

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);
    },
});

這會將用戶名字段添加到您的登錄/注冊表單,並在注冊新用戶時檢查用戶名沖突。

頂部是服務器代碼,底部是通用代碼,因此您可以將整個代碼段放置在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

您需要使用. 訪問對象的屬性,而不是空間

Meteor.users.findOne

您提供的信息很少,無法發現問題。 這就是說,您如何讓用戶進入?

請注意,OAuth注冊不會在創建的用戶對象上提供username段,也不會僅基於電子郵件地址提供密碼注冊。 在這些情況下, user.username將是undefined

我建議確保您所有的用戶都獲得一個profile.username (可能利用一些Accounts.onLogin掛鈎或某個配置文件頁面來強迫用戶選擇用戶名。

之后,您將擴展您的帖子元數據設置author: user.profile.username

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM