Chr*_*ris 1 user-accounts meteor
我在我的流星应用程序的首页上添加了一个表单,其中包含一封电子邮件和用户名.它会发送一个注册电子邮件,其中包含一个用于创建密码的链接.创建帐户时需要添加许多后台配置文件设置,并且它可以通过{{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>
Run Code Online (Sandbox Code Playgroud)
页面事件:
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');
}
});
}
Run Code Online (Sandbox Code Playgroud)
});
注册方法(在服务器中):
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;
}
}
});
Run Code Online (Sandbox Code Playgroud)
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;
});
Run Code Online (Sandbox Code Playgroud)
您profile打电话时没有提供字段Accounts.createUser(options).所以当你的onCreateUser()函数测试时:
if (options.profile) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
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;
});
Run Code Online (Sandbox Code Playgroud)
另请注意,profile按惯例,它适用于用户可修改的字段,默认情况下会发布给用户.因此,您添加到其中的大多数字段profile应该可以直接添加到用户对象中,也可以添加到您选择的单独用户字段中.来自Meteor文档:
profile:一个(默认情况下)用户可以使用任何数据创建和更新的对象....默认情况下,当前用户的username,emails并profile发布到客户端.
| 归档时间: |
|
| 查看次数: |
1565 次 |
| 最近记录: |