如何向Meteor.users集合添加自定义字段?
对不起我的英语。我使用useraccounts包:bootstrap进行登录、注册等。如何在注册后向Meteor.users集合添加任意数据。例如,我希望注册后的用户具有值为“false”的字段“status”,或具有注册时间的字段“time”。谢谢。useraccounts:bootstrap为您提供了一种自定义注册面板模板的方法,方法是向注册表单中添加可见、明确和可编辑的字段,如useraccounts/core的GitHub文档中所述(查找AccountTemplates.addFields方法) 但是,useraccounts:bootstrap依赖于accounts密码,因此您可以使用其accounts.createUser方法,只需将对象中的附加字段传递到accounts.createUser方法。您的createUser方法如下所示:如何向Meteor.users集合添加自定义字段?,meteor,user-accounts,meteor-accounts,meteor-useraccounts,Meteor,User Accounts,Meteor Accounts,Meteor Useraccounts,对不起我的英语。我使用useraccounts包:bootstrap进行登录、注册等。如何在注册后向Meteor.users集合添加任意数据。例如,我希望注册后的用户具有值为“false”的字段“status”,或具有注册时间的字段“time”。谢谢。useraccounts:bootstrap为您提供了一种自定义注册面板模板的方法,方法是向注册表单中添加可见、明确和可编辑的字段,如useraccounts/core的GitHub文档中所述(查找AccountTemplates.addField
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
});
流星论坛上讨论了这个问题:
解决问题的一种更优雅的方法是在每次创建用户帐户时调用服务器端函数Accounts.onCreateUser。此函数将为新创建的帐户分配注册时间和状态。在Meteor的docs:Accounts.onCreateUser中选中此项。如果用户需要提供数据,则需要添加所需的字段 在服务器上,您可以附加
onCreateUser()
回调以在创建新用户时设置数据
从“lodash”导入;
Accounts.onCreateUser((选项,用户)=>{
//在此处添加额外字段;如果需要,不要忘记验证选项
_.扩展(用户、{
状态:false,
createdAt:新日期()
});
返回用户;
});
options
参数包含来自客户端的数据。以下是我的操作方法;与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;
});
我们鼓励链接到外部资源,但请在链接周围添加上下文,以便您的其他用户了解它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防无法访问目标站点或永久脱机。