【问题标题】:Meteor's onCreateUser not firing when running createUser from server从服务器运行 createUser 时 Meteor 的 onCreateUser 未触发
【发布时间】:2014-05-17 11:05:30
【问题描述】:

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

注册表

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

【问题讨论】:

  • 你没有发布你的onCreateUser函数。您是否在节点控制台中收到任何错误?
  • @Cuberto 已发布,抱歉我忘记了。在浏览器控制台中没有收到任何节点控制台错误或错误。

标签: meteor user-accounts


【解决方案1】:

当您调用 Accounts.createUser(options) 时,您没有提供 profile 字段。因此,当您的 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会发布到客户端。

【讨论】:

  • 很棒的提示 - 这对我帮助很大。谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多