如何在Meteor中创建用户期间获取用户ID?

Bar*_*yle 5 creation user-accounts email-verification meteor

我正在使用Meteor启动功能在服务器上创建默认用户.我想创建一个用户并在启动时验证他/她的电子邮件(我假设您只能在创建帐户后执行此操作).

这就是我所拥有的:

Meteor.startup(function() {
  // Creates default accounts if there no user accounts
  if(!Meteor.users.find().count()) {
    //  Set default account details here
    var barry = {
      username: 'barrydoyle18',
      password: '123456',
      email: 'myemail@gmail.com',
      profile: {
        firstName: 'Barry',
        lastName: 'Doyle'
      },
      roles: ['webmaster', 'admin']
    };

    //  Create default account details here
    Accounts.createUser(barry);

    Meteor.users.update(<user Id goes here>, {$set: {"emails.0.verified": true}});
  }
});
Run Code Online (Sandbox Code Playgroud)

正如我所说,我假设在将verify标记设置为true之前必须首先创建用户(如果此语句为false,请显示在创建用户时使标志为true的解决方案).

为了将电子邮件验证标志设置为true,我知道我可以在创建后使用更新用户Meteor.users.update(userId, {$set: {"emails.0.verified": true}});.

我的问题是,我不知道如何获取我新创建的用户的userID,我该怎么做?

Bre*_*ain 7

您应该能够访问从Accounts.createUser()函数返回的用户标识:

var userId = Accounts.createUser(barry);
Meteor.users.update(userId, {
    $set: { "emails.0.verified": true}
});
Run Code Online (Sandbox Code Playgroud)

或者,您可以通过Accounts.onCreateUser()函数访问新创建的用户:

var barry = {
  username: 'barrydoyle18',
  password: '123456',
  email: 'myemail@gmail.com',
  profile: {
    firstName: 'Barry',
    lastName: 'Doyle'
  },
  isDefault: true, //Add this field to notify the onCreateUser callback that this is default
  roles: ['webmaster', 'admin']
};

Accounts.onCreateUser(function(options, user) {
    if (user.isDefault) {
        Meteor.users.update(user._id, {
            $set: { "emails.0.verified": true}
        });
    }
});
Run Code Online (Sandbox Code Playgroud)