如何将其他字段添加到用户集合中。我了解选项对象允许四个字段 - 用户名、电子邮件密码和个人资料。那么在 Accounts.onCreateUser 上,有没有办法在根级别(不在配置文件字段内)添加其他字段?
截至目前,解决方案是使用 Accounts.createUser 在配置文件中添加字段,将此字段复制到根级别,然后使用 Accounts.onCreateUser 删除配置文件中的字段。这是在下面的示例中为“userType”完成的
客户端.js
Template.joinForm.events({
'submit .form-join': function(e, t) {
e.preventDefault();
var firstName = t.find('#firstName').value,
lastName = t.find('#lastName').value,
email = t.find('#email').value,
password = t.find('#password').value,
username = firstName + '.' + lastName,
profile = {
name: firstName + ' ' + lastName,
userType: selectedUserType // this is copied to root level and deleted from profile.
};
Accounts.createUser({
//NEWFIELD1: [],
//NEWFILED2: [],
email: email,
username: username,
password: password,
profile: profile
}, function(error) {
if (error) {
alert(error);
} else {
Router.go('/');
}
});
}
});
服务器.js
Accounts.onCreateUser(function(options, user) {
if (options.profile) {
if (options.profile.userType) {
user.userType = options.profile.userType;
delete options.profile.userType;
}
user.profile = options.profile;
}
return user;
});