【发布时间】:2016-09-01 09:54:02
【问题描述】:
对不起我的英语。我使用包 useraccounts:bootstrap 进行登录、注册等。注册后如何将任意数据添加到 Meteor.users 集合。例如,我希望注册后的用户有一个值为“false”的字段“status”或带有注册时间的字段“time”。谢谢。
【问题讨论】:
标签: meteor user-accounts meteor-accounts meteor-useraccounts
对不起我的英语。我使用包 useraccounts:bootstrap 进行登录、注册等。注册后如何将任意数据添加到 Meteor.users 集合。例如,我希望注册后的用户有一个值为“false”的字段“status”或带有注册时间的字段“time”。谢谢。
【问题讨论】:
标签: meteor user-accounts meteor-accounts meteor-useraccounts
这就是我的做法;匹配meteor docs风格,不需要lodash:
import { Accounts } from 'meteor/accounts-base';
Accounts.onCreateUser((options, user) => {
const userToCreate = Object.assign({
status: false,
createdAt: new Date(),
}, user);
if (options.profile) userToCreate.profile = options.profile;
return userToCreate;
});
【讨论】:
useraccounts:bootstrap 为您提供了一种自定义注册面板模板的方法,方法是在注册表单中添加可见、明确和可编辑的字段,如 useraccounts/core 的 GitHub 文档中所述(查找 AccountTemplates.addFields 方法)。
但是,useraccounts:bootstrap 依赖于 accounts-password,因此您可以使用它的 Accounts.createUser 方法,只需传递额外的传递给 Accounts.createUser 方法的对象中的字段。您的 createUser 方法如下:
Accounts.createUser({
username:'newuser',
password:'pass1234',
profile:{ //no sensitive data here, this can be modified by the user
},
registrationTime: new Date, //date & time of registration
status: false
});
这个问题在 Meteor 论坛上讨论过:forums.meteor.com。
解决问题的一种更优雅的方法是在每次创建用户帐户时调用服务器端函数Accounts.onCreateUser。此函数会将注册时间和状态分配给新创建的帐户。查看 Meteor 的文档:Accounts.onCreateUser docs.meteor.com
【讨论】:
如果用户需要提供数据,您需要customize the UI 并添加所需的字段。
在服务器上,您可以附加一个onCreateUser() 回调来设置新用户创建时的数据。
import _ from 'lodash';
Accounts.onCreateUser((options, user) => {
// add your extra fields here; don't forget to validate the options, if needed
_.extend(user, {
status: false,
createdAt: new Date()
});
return user;
});
options 参数包含来自客户端的数据。
【讨论】: