Meteor 如何向用户集合添加额外属性?
我正在使用Meteor 如何向用户集合添加额外属性?,meteor,meteor-accounts,Meteor,Meteor Accounts,我正在使用Accounts.createUser向数据库添加新用户,但问题是,并不是所有属性都被添加 以下是我添加新用户的代码: import {Accounts} from 'meteor/accounts-base'; Template.addingUser.events({ 'submit #addUser': function (e, t) { e.preventDefault(); Session.set('name', t.find('#
Accounts.createUser
向数据库添加新用户,但问题是,并不是所有属性都被添加
以下是我添加新用户的代码:
import {Accounts} from 'meteor/accounts-base';
Template.addingUser.events({
'submit #addUser': function (e, t) {
e.preventDefault();
Session.set('name', t.find('#name').value);
Session.set('email', t.find('#email').value);
Session.set('telephoneOffice', t.find('#telephoneOffice').value);
Session.set('telephoneHouse', t.find('#telephoneHouse').value);
Session.set('salary', t.find('#salary').value);
let userId = Accounts.createUser({
username: Session.get('name'),
password: "123456",
email: Session.get('email'),
telephoneOffice: Session.get('telephoneOffice'),
telephoneHouse: Session.get('telephoneHouse'),
employeeSalary: Session.get('salary'),
annualLeave: 14
}, function (err) {
if (err)
console.log(err);
else
console.log('It worked...');
});
Accounts.sendEnrollmentEmail(userId);
}
});
只添加名称、电子邮件和密码
如何包括其他信息,如
telephoneOffice
?帐户。createUser
不接受用户名、电子邮件、密码和配置文件以外的自定义参数。传递自定义用户信息的默认功能是传递那些字段,例如telephoneOffice
,作为profile
对象的一部分,该对象复制到插入到用户集合的文档中的user.profile
例如:
let userId = Accounts.createUser({
username: Session.get('name'),
password: "123456",
email: Session.get('email'),
profile: {
telephoneOffice: Session.get('telephoneOffice'),
telephoneHouse: Session.get('telephoneHouse'),
employeeSalary: Session.get('salary'),
annualLeave: 14
}
});
请注意,user.profile
字段为。所以它是遗留下来的,但Meteor实际上建议避免将其用于存储
如果希望这些字段位于user
而不是user.profile
上,可以做的是如上所述在profile
对象上传递自定义参数,然后使用重写默认行为。大概是这样的:
Accounts.onCreateUser(function(options, user) {
if (options.profile)
_.extend(user, options.profile);
return user;
});
请参阅此处的更多信息:
帐户。createUser
不接受用户名、电子邮件、密码和配置文件以外的自定义参数。传递自定义用户信息的默认功能是传递那些字段,例如telephoneOffice
,作为profile
对象的一部分,该对象复制到插入到用户集合的文档中的user.profile
例如:
let userId = Accounts.createUser({
username: Session.get('name'),
password: "123456",
email: Session.get('email'),
profile: {
telephoneOffice: Session.get('telephoneOffice'),
telephoneHouse: Session.get('telephoneHouse'),
employeeSalary: Session.get('salary'),
annualLeave: 14
}
});
请注意,user.profile
字段为。所以它是遗留下来的,但Meteor实际上建议避免将其用于存储
如果希望这些字段位于user
而不是user.profile
上,可以做的是如上所述在profile
对象上传递自定义参数,然后使用重写默认行为。大概是这样的:
Accounts.onCreateUser(function(options, user) {
if (options.profile)
_.extend(user, options.profile);
return user;
});
请参阅此处的更多信息:您需要在
配置文件
对象中传递额外的数据
Accounts.createUser({
username: Session.get('name'),
password: "123456",
email: Session.get('email'),
profile: {
telephoneOffice: Session.get('telephoneOffice'),
telephoneHouse: Session.get('telephoneHouse'),
employeeSalary: Session.get('salary'),
annualLeave: 14
}
...
您需要在
profile
对象中传递额外的数据
Accounts.createUser({
username: Session.get('name'),
password: "123456",
email: Session.get('email'),
profile: {
telephoneOffice: Session.get('telephoneOffice'),
telephoneHouse: Session.get('telephoneHouse'),
employeeSalary: Session.get('salary'),
annualLeave: 14
}
...