我想知道如何在 Meteor 中更改用户配置文件信息。我使用 accounts-base 包创建了一个应用程序,因此我可以快速管理所有与用户帐户相关的东西。这真的很棒。
在官方文档中说:
profile:一个对象(默认情况下)用户可以创建和更新任何数据。
但我怎么能让用户改变它?
关于同一主题,默认情况下使用{{loginButtons}}
标签,当用户登录时,我得到以下图像:
有没有可能添加Change profile
,Change email
或类似的东西?
谢谢
目前accounts-ui
没有内置的更改配置文件按钮,您必须手动进行。
例如,如果你这样做
Meteor.users.update({_id:Meteor.user()._id}, {$set:{"profile.name":"Carlos"}})
您可以在上面的accounts-ui
中更改屏幕,您必须显示一个名称,而不是单击以显示上面的对话框的电子邮件。
电子邮件有点棘手,您必须从服务器执行此操作,因为(在 metal.methods / call 中)您无法从客户端修改电子邮件内容,我建议添加新电子邮件并进行验证,而不是更改现有电子邮件(因为它也是他们的登录名)。或者先验证然后更改它,以免将某人的电子邮件更改为无法恢复密码的东西。
Meteor.users.update({_id:Meteor.user()._id}, {$addToSet:{"emails":{address:"newemail@newemail.com","verified":false}}});
或者,如果您希望用户拥有一封电子邮件,他们可以更改:
Meteor.users.update({_id:Meteor.user()._id}, {$set:{"emails":[{address:"newemail@newemail.com"}]});
要扩展这个答案,最好将此代码放入(Validated)Meteor 方法。
使用CallPromiseMixin在前端使用 async / await 语法调用此方法
使用simpl-schema验证用户名参数
// imports/api/methods.js
const NO_SPECIAL_CHARACTERS_REGEX = /^[^`~!@#$%^&*()_|+=?;:'"<>{}\[\]\\/]*$/;
export const updateProfileName = new ValidatedMethod({
name: 'users.updateProfileName',
mixins: [CallPromiseMixin],
validate: new SimpleSchema({
name: { type: String, regEx: NO_SPECIAL_CHARACTERS_REGEX, min: 1, max: 50 },
}).validator(),
run({ name }) {
if (!this.userId) {
throw new Meteor.Error('User needs to be signed in to call this method');
}
return Meteor.users.update({ _id: this.userId }, { $set: { 'profile.name': name } });
},
});
然后在前端
// imports/ui/changeName.js
Template.changeName.events({
// ...
async 'submit #change-name-form'(event, instance) {
event.preventDefault();
try {
await updateProfileName.callPromise({ name: instance.$('#profile-name').val() });
} catch (e) {
// do something with the error
}
},
});
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(59条)