Meteor 添加额外的用户字段
在我的Meteor应用程序中,我使用默认的Meteor 添加额外的用户字段,meteor,Meteor,在我的Meteor应用程序中,我使用默认的accounts软件包,它为我提供了默认的登录和注册功能。现在,我想给用户添加一个额外的字段,比如说昵称,并允许登录用户编辑此信息 为了编辑配置文件,我想我应该这样做: Template.profileEdit.events({ 'submit form': function(e) { e.preventDefault(); if(!Meteor.user()) throw new Meteor.Error(401, "
accounts
软件包,它为我提供了默认的登录和注册功能。现在,我想给用户添加一个额外的字段,比如说昵称
,并允许登录用户编辑此信息
为了编辑配置文件,我想我应该这样做:
Template.profileEdit.events({
'submit form': function(e) {
e.preventDefault();
if(!Meteor.user())
throw new Meteor.Error(401, "You need to login first");
var currentUserId = this._id;
var user = {
"profile.nickname": $(e.target).find('[name=nickname]').val()
};
Meteor.users.update(currentUserId, {
$set: user
}, function(error){
if(error){
alert(error.reason);
} else {
Router.go('myProfile', {_id: currentUserId});
}
});
}
});
但是如果我在Mongo中查找,我不会存储信息。同样在显示配置文件时,{{profile.昵称}
返回空。这里怎么了
编辑:添加collections\users.js
以显示权限:
Meteor.users.allow({
update: function (userId, doc) {
if (userId && doc._id === userId) {
return true;
}
}
});
Meteor.users.deny({
update: function(userId, user, fieldNames) {
return (_.without(fieldNames, 'profile.nickname').length > 0);
}
});
是的,我认为应该这样做,尽管我还没有实际运行代码。这个想法当然是正确的 需要注意的主要事项有:
Meteor.users.allow()
块允许从客户端编辑用户文档的必要性,假设您要删除“不安全”包(在生产中执行任何操作之前需要删除该包)Meteor.publish
函数并订阅它(同样,您确实应该这样做)这是可行的,但当我查看数据库时,我没有看到存储的
全名
属性?您是否使用meteor mongo
shell查看它?是的,我已经更新了上面的profile\u edit.js
。显示配置文件时,{{profile.name}
为空。这可能是权限问题吗?是否启用了autopublish
,如果没有,是否具有发布功能?实际上,你不需要一个人在profile