我在向Meteor用户对象(Meteor.user)添加自定义用户字段时遇到问题.我希望用户有一个"状态"字段,我宁愿不将它嵌套在"profile"(即profile.status)下,我知道默认情况下是r/w.(我已经删除了autopublish.)
我已经能够将该字段发布到客户端了
Meteor.publish("directory", function () {
return Meteor.users.find({}, {fields: {username: 1, status: 1}});
});
Run Code Online (Sandbox Code Playgroud)
...但我无法获得允许登录用户更新自己的权限status.
如果我做
Meteor.users.allow({
update: function (userId) {
return true;
}});
Run Code Online (Sandbox Code Playgroud)
在Models.js,用户中可以编辑每个用户的所有字段.这不酷.
我试过做变种,比如
Meteor.users.allow({
update: function (userId) {
return userId === Meteor.userId();
}});
Run Code Online (Sandbox Code Playgroud)
和
Meteor.users.allow({
update: function (userId) {
return userId === this.userId();
}});
Run Code Online (Sandbox Code Playgroud)
他们只是在控制台中找到了Access Denied错误.
该文件解决了几分这一点,但不会进入足够的细节.我犯的是什么愚蠢的错误?
(这与此 SO问题类似,但该问题仅涉及如何发布字段,而不是如何更新字段.)
这就是我开始工作的方式.
在服务器中,我发布了userData
Meteor.publish("userData", function () {
return Meteor.users.find(
{_id: this.userId},
{fields: {'foo': 1, 'bar': 1}}
);
});
Run Code Online (Sandbox Code Playgroud)
并设置允许如下
Meteor.users.allow({
update: function (userId, user, fields, modifier) {
// can only change your own documents
if(user._id === userId)
{
Meteor.users.update({_id: userId}, modifier);
return true;
}
else return false;
}
});
Run Code Online (Sandbox Code Playgroud)
在客户端代码中,我更新用户记录的某个地方,只有在有用户的情况下
if(Meteor.userId())
{
Meteor.users.update({_id: Meteor.userId()},{$set:{foo: 'something', bar: 'other'}});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2861 次 |
| 最近记录: |