简体   繁体   English

在Meteor.JS中创建第一个用户创建第二个用户

[英]Creating Second User on Creating First User in Meteor.JS

How can we automatically create a second user when the user registers (the first user) from a form generated using the useraccounts:core package? 当用户从使用useraccounts:core包生成的表单注册(第一个用户)时,我们如何自动创建第二个用户?

Running a Accounts.createUser from within Accounts.onCreateUSer causes an error Exception while invoking method 'ATCreateUserServer' TypeError: Cannot read property '_id' of undefined 运行Accounts.createUser从内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
    }

});

It looks to me like you're conflating the user argument and the options argument. 在我看来,你正在混淆user参数和options参数。 For instance, the type field comes in through the options argument, not user . 例如, type字段通过options参数进入,而不是user

The following code worked for me: 以下代码对我有用:

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

I then tested like so: 然后我测试了这样:

// 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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM