首次启动时创建完整的用户配置文件

时间:2016-04-28 01:37:35

标签: meteor

当我第一次将meteor应用程序加载到计算机上时,我想创建一些虚拟帐户。这是出于开发目的。我已经开始工作但是我无法弄清楚如何将数据添加到子模式。

DB

  "profile": {
    "firstName": "first",
    "familyName": "last",
    "phone": "041111111",
    "address": {
      "street": "22 Test St",
      "suburb": "Melbourne",
    }
  }

工作版本缺少信息

 // user creation
    _.each(users, function(userData) {
      // return id for use in roles assignment below
      var userId = Accounts.createUser({
        email: userData.email,
        password: 'password',
        profile: {
          firstName: userData.firstName,
          familyName: userData.familyName,
          phone: '041111111',
        },
      });

无效 - 我想添加用户地址

   // user creation
    _.each(users, function(userData) {
      // return id for use in roles assignment below
      var userId = Accounts.createUser({
        email: userData.email,
        password: 'Zaq12wsx',
        profile: {
          firstName: userData.firstName,
          familyName: userData.familyName,
          phone: '0416089930',
          address: {
            street: '22 Test St',
            suburb: 'Melbourne'
          }
        },
      });

SimpleSchema

Schema.UserProfile = new SimpleSchema({
    firstName: {
        type: String,
        optional: false,
    },
    familyName: {
        type: String,
        optional: false,
    },
    address: {
        type: Schema.Address,
        optional: true
    }
});

Schema.Address = new SimpleSchema({
    street: {
        type: String,
        optional: false,
    },
    suburb: {
        type: String,
        optional: false,
    },
});

1 个答案:

答案 0 :(得分:0)

根据您的评论,您为用户设计了错误的架构。这就是为用户设计模式的方法。

用户架构

Schema.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
    }
    profile: {
        type: Schema.UserProfile,
        optional: true
    },
    services: {
        type: Object,
        optional: true,
        blackbox: true
    },
});

用户个人资料的架构

Schema.UserProfile = new SimpleSchema({
    firstName: {
        type: String,
        optional: false,
    },
    familyName: {
        type: String,
        optional: false,
    },
    address: {
        type: Schema.Address,
        optional: true
    }
});

用户个人资料地址的架构。

Schema.Address = new SimpleSchema({
    street: {
        type: String,
        optional: false,
    },
    suburb: {
        type: String,
        optional: false,
    },
});

然后您可以将用户架构添加到像这样的meteor.users

Meteor.users.attachSchema(Schema.User);

您也可以像这样定义单个对象中的所有架构

Schema.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
    }
    profile: {
        type: Object,
        optional: true
    },
    'profile.firstName': {
        type: String,
        optional: false,
    },
    'profile.familyName': {
        type: String,
        optional: false,
    },
    'profile.address': {
        type: Object,
        optional: true
    },
    'profile.address.street': {
        type: String,
        optional: false,
    },
    'profile.address.suburb': {
        type: String,
        optional: false,
    },
    services: {
        type: Object,
        optional: true,
        blackbox: true
    },
});