从服务器运行createUser时,Meteor的onCreateUser不会触发

时间:2014-04-04 23:17:53

标签: meteor user-accounts

我已经在我的流星应用程序的首页上添加了一个表单,其中包含一封电子邮件和用户名。它会发送一个注册电子邮件,其中包含一个用于创建密码的链接。创建帐户时需要添加许多后台配置文件设置,并且它可以通过{{loginButtons}}帐户创建来运行。出于某种原因,onCreateUser在从createUser调用时不起作用。我哪里出错了?提前谢谢。

注册表

<form role="form" class="form-horiztonal" id="signup">
            <div class="form-group">
                <label for="name" class="col-sm-3 control-label">Name</label>
                <div class="col-sm-9">
                    <input name="name" id="name" class="form-control" type="text" placeholder="Name"/>
                </div>
            </div>

            <div class="form-group">
                <label for="email" class="col-sm-3 control-label">Email</label>
                <div class="col-sm-9">
                    <input name="email" class="form-control" type="email" placeholder="example@gmail.com"/>
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-9 col-sm-offset-3">
                    <input type="submit" value="Get Started!" class="btn btn-success"/>
                </div>
            </div>
        </form>

页面活动

 Template.landing.events({
'submit form#signup' : function(e) {
    e.preventDefault();

    var user = {
        name: $(e.target).find('[name=name]').val(),
        email: $(e.target).find('[name=email]').val()
    }

    Meteor.call('signup', user, function(error, id) {
        if (error) {
                // display the error to the user
                console.log(error);
            } else {
                Router.go('awesome');
            }
        });

}

});

注册方法(在服务器中)

Meteor.methods({
signup: function(user) {

// ensure the user is logged in
if (!user)
  throw new Meteor.Error(401, "Can't make a user without a user object");

// ensure the user has a name
if (!user.name)
  throw new Meteor.Error(422, 'Please fill in a name');

// ensure there is an email
if (!user.email)
  throw new Meteor.Error(424, 'Please fill in an email address');  

var userId = Accounts.createUser(user);
if(userId) {
Accounts.sendEnrollmentEmail(userId);
return userId;
}
}
 });

onCreateUser(在服务器中)

Accounts.onCreateUser(function(options, user) {
    if (options.profile) {
        user.profile                        =   options.profile;                //Copy initial profile settings
        //Set roles
        user.roles                          =   ["User"];


        //Account settings
        user.profile.active                 =   false;                          //Account is not active on creation
        user.profile.trial                  =   true;                           //Accounts have 1 month trial
        user.profile.expiration             =   moment().add('M', 1);           //No expiration date on unactivated accounts

        user.profile.bill                   =   null;                           //Bill monthly, yearly
        user.profile.ppId                   =   null;                           //Paypal Id for associated credit card

        user.profile.resources              =   0;                              //Number of resources an account has created
        user.profile.resourceLimit          =   2;                              //Limit for the number of resources an account can have

        //User settings
        user.profile.name                   =   null;                           //Name is blank when created
        user.profile.phone                  =   null;                           //Phone is blank when created
        user.profile.createdOn              =   moment().toISOString();         //Createdon tracks when account created
    }

    return user;
});

1 个答案:

答案 0 :(得分:6)

致电profile时,您未提供Accounts.createUser(options)字段。因此,当您的onCreateUser()函数测试时:

if (options.profile) {
  // ...
}

options.profile不存在,并且未添加任何自定义字段。尝试将代码更改为以下内容:

Accounts.onCreateUser(function(options, user) {
  // Use provided profile in options, or create an empty profile object
  user.profile = options.profile || {};
  user.roles = ["User"];

  // Add additional fields

  return user;
});

另请注意,profile按惯例适用于用户可修改的字段,默认情况下会发布给用户。因此,您添加到profile的大多数字段可能应该直接添加到用户对象中,也可以添加到您选择的单独用户字段中。来自Meteor文档:

  

profile:一个(默认情况下)用户可以使用任何数据创建和更新的对象。   ...   默认情况下,当前用户的usernameemailsprofile会发布到客户端。