在Meteor.JS创build第一个用户创build第二个用户

当用户从使用useraccounts:core软件包生成的表单中注册(第一个用户)时,我们如何自动创build第二个用户?

Exception while invoking method 'ATCreateUserServer' TypeError: Cannot read property '_id' of undefinedAccounts.onCreateUSer运行Accounts.onCreateUSer会导致错误Exception while invoking method 'ATCreateUserServer' TypeError: Cannot read property '_id' of undefined

 Accounts.onCreateUser(function(options, user) { // Create Primary User if(!user.type) { // Set user.type as 'user' user.type = 'user' // Create Secondary User Accounts.createUser({ username: options.profile.slaveName, password: options.profile.slaveName, type: 'slave', profile: { firstName: user.profile.firstName, lastName: user.profile.lastName } }) user.profile = options.profile return user } // Create Secondary User if(user.type == 'slave') { user.profile = options.profile return user } }); 

它看起来像你在混淆user参数和options参数。 例如, type字段通过options参数进入,而不是user

下面的代码为我工作:

 Accounts.onCreateUser(function(options, user) { // Create Primary User if(!options.type) { // Set user.type as 'user' options.type = 'user' // Create Secondary User Accounts.createUser({ username: options.profile.slaveName, password: options.profile.slaveName, type: 'slave', profile: { firstName: options.profile.firstName, lastName: options.profile.lastName } }); user.profile = options.profile return user } // Create Secondary User if(options.type == 'slave') { user.profile = options.profile return user } }); 

然后我testing如下:

 // console Accounts.createUser({username: "guest", password: "guest", profile: {slaveName: 'guestslave', firstName: "Greatest", lastName: "Ever"}}) Meteor.users.find({username: {$regex: 'guest'}}).fetch() > [returned two user objects]