3

我一直在努力扩展 Meteor.users 架构,它工作正常,但是在我在 Meteor.startup() 中创建第一个用户并尝试登录后,我收到“找不到用户”错误。即使我可以在 mongo shell 中看到用户。这是我的架构:

    Schemas.User = new SimpleSchema({
    username: {
        type: String,
        optional: true
    },
    emails: {
        type: Array,
        optional: true
    },
    "emails.$": {
        type: Object
    },
    "emails.$.address": {
        type: String,
        regEx: SimpleSchema.RegEx.Email
    },
    "emails.$.verified": {
        type: Boolean
    },
    createdAt: {
        type: Date,
        optional: true
    },
    "firstName":{
     type: String,
     max: 50,
     min:2
     },
    'middleName':{
    type: String,
    optional: true,
    max: 50
    },
    "lastName":{
     type: String,
     max: 50,
     min:2
    },
    "gender": {
    type: String,
    autoform: {
      afFieldInput: {type: "select-radio-inline",},
      options: 
      [
         {label: "Male", value: "Male"},
         {label: "Female", value: "Female"}

      ]
    }
    },
   "branch": {
      type: String,
      optional: true,
      autoform: {
        type: "select",
        options: function () {
            return CompanyBranches.find().map(function (c) {
                return {label: c.companyName+' - '+c.addressCity, value: c._id};
            });
        }
    }
    },
    "active":{
    type: String,
    allowedValues: ["Yes","No"],
    autoform: {
      options: [
        {label: "Yes", value: "Yes"},
        {label: "No", value: "No"}
      ],
      afFieldInput: {
       type: "select-radio-inline"
      }
    }
  },
    services: {
        type: Object,
        optional: true,
        blackbox: true
    },
    roles: {
        type: [String],
        optional: true,
        autoform: {
          options: [
            {label: "Dashboard", value: "Dashboard"},
            {label: "Branches", value: "Branches"},
            {label: "User Profile", value: "User Profile"},
            {label: "Case Managers", value: "Case Managers"},
            {label: "Insurance Company", value: "Insurance Company"},
            {label: "Tasks", value: "Tasks"},
            {label: "Calendar", value: "Calendar"},
            {label: "Contacts", value: "Contacts"},
            {label: "Cases", value: "Cases"},
            {label: "Requests", value: "Requests"},
            {label: "Accounts", value: "Accounts"},
            {label: "Reports", value: "Reports"},
            {label: "Search", value: "Search"},
            {label: "HR", value: "HR"}
          ],
          afFieldInput: {
           type: "select-checkbox-inline"
          }
        }
    }
    });

    Meteor.users.attachSchema(Schemas.User);

这是我的流星启动功能:

Meteor.startup(function () {
if ( Meteor.users.find().count() === 0 ) {
   Accounts.createUser({ 
        username: "leocrawf@gmail.com",
        email:"leocrawf@gmail.com",
        password: "leoten",
        firstName: "Leocrawf",
        lastName: "Stewart",
        gender:"Male",
        active: "Yes"
      }, function (error) {
        if (error) {
          console.log("Cannot create user");
        }
      });

   }
});

在服务器上,我在 Accounts.onCreateUser() 中执行此操作:

Accounts.onCreateUser(function(options, user) {
        user = options || {};
        user.username = options.username;
        user.firstName = options.firstName;
        user.lastName  = options.lastName;   
        user.active    = options.active;
        user.gender    = options.gender;
        // Returns the user object
        return user;
    });

当我查询 mongo shell 时,我得到了这个:

meteor:PRIMARY> db.users.find().pretty()
{
"_id" : "pFurR8iDYWJcX9rus",
"username" : "leocrawf@gmail.com",
"firstName" : "Leocrawf",
"lastName" : "Stewart",
"gender" : "Male",
"active" : "Yes",
"services" : {
    "resume" : {
     "loginTokens" : [
            {
    "when" : ISODate("2016-02-05T23:13:38.364Z"),
    "hashedToken" : "vs5xVlKL59yVTO/fbKbSnar38I8ILAruj2W1YecQ2Io="
            }
        ]
     }
   }
 }
4

1 回答 1

1

创建用户时,您似乎没有正确设置配置文件:

代替:

Accounts.createUser({ 
  username: "leocrawf@gmail.com",
  email:"leocrawf@gmail.com",
  password: "leoten",
  firstName: "Leocrawf",
  lastName: "Stewart",
  gender:"Male",
  active: "Yes"
},

尝试:

Accounts.createUser({ 
  username: "leocrawf@gmail.com",
  email: "leocrawf@gmail.com",
  password: "leoten",
  profile: {
    firstName: "Leocrawf",
    lastName: "Stewart",
    gender: "Male",
    active: "Yes"
  }
},

我还建议active: true不要active: "Yes"使用布尔值而不是字符串。顺便说一句,它看起来不像你已经定义activegender在你的架构中。

于 2016-02-06T01:17:06.757 回答