2016-04-28 56 views
0

我想在我第一次将我的流星应用程序加载到计算机上时创建一些虚拟帐户。这是为了发展目的。我有这个工作,但我不知道如何将数据添加到子模式。第一次启动时创建一个完整的用户配置文件

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

难道你为用户定义任何模式。使用简单的收集或收集2? –

+0

是的,我正在使用collection2 – bp123

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

我不知道我按照你的建议'Meteor.users.attachSchema(Schema.User);'你能告诉我我将如何使用它来添加街道和郊区? – bp123

相关问题