如何向用户集合添加额外的属性?
How to add extra attributes to users collection?
我正在使用 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
?
Accounts.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
字段是 by default modifiable by users。所以它是遗留的,但 Meteor 实际上建议避免使用它进行存储。
如果您希望这些字段位于 user
而不是 user.profile
,您可以做的是像上面那样将自定义参数传递给 profile
对象,然后覆盖使用 Accounts.onCreateUser 的默认行为。像这样:
Accounts.onCreateUser(function(options, user) {
if (options.profile)
_.extend(user, options.profile);
return user;
});
在此处查看更多信息:https://guide.meteor.com/accounts.html#custom-user-data
您需要在 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
}
...
我正在使用 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
?
Accounts.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
字段是 by default modifiable by users。所以它是遗留的,但 Meteor 实际上建议避免使用它进行存储。
如果您希望这些字段位于 user
而不是 user.profile
,您可以做的是像上面那样将自定义参数传递给 profile
对象,然后覆盖使用 Accounts.onCreateUser 的默认行为。像这样:
Accounts.onCreateUser(function(options, user) {
if (options.profile)
_.extend(user, options.profile);
return user;
});
在此处查看更多信息:https://guide.meteor.com/accounts.html#custom-user-data
您需要在 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
}
...