gpt4 book ai didi

javascript - 尝试更新 Meteor.users 结果为 "Access Denied"

转载 作者:行者123 更新时间:2023-11-29 18:02:12 25 4
gpt4 key购买 nike

我正在做一个 Meteor 项目,我正在尝试更新特定用户的个人资料。但是,我总是收到

    update failed: Access denied

我将用户 ID 存储在变量 user_id 中,我的代码如下所示

    Meteor.users.update({_id: user_id}, {$set: {'profile.participate_studies': updated_user_list}}) 

但是,如果我这样做

    Meteor.users.update({_id: Meteor.userId()}, {$set: {'profile.participate_studies': updated_user_list}})

更新有效。这让我觉得我存储的 ID 存在一些问题。 Meteor.userId() 返回的内容和包含 id 的字符串之间有区别吗?我知道 id 是正确的,因为我可以使用 id 在 shell 中访问它。

我看过Updating Meteor.users from client ,但我没有尝试更新编辑基本用户对象。非常感谢您的帮助!

最佳答案

您可以更新自己的个人资料,所以当您这样做时

Meteor.users.update({_id: Meteor.userId()}, {
$set: {'profile.participate_studies': updated_user_list}
})

您更新自己的个人资料(Meteor.userId() 返回当前登录用户的 ID)。

当您更新其他用户的个人资料时,您会收到访问被拒绝错误。

你需要为Meteor.users设置allow规则:

Meteor.users.allow({
update: function(userId, user) {
return true;

/**
* Don't use `return true` in production!
* You probably need something like this:
* return Meteor.users.findOne(userId).profile.isAdmin;
*/
}
});

允许规则的完整文档:http://docs.meteor.com/#/full/allow


提示:您可以使用简写形式按 id 进行更新:

Meteor.users.update(Meteor.userId(), {...})

关于javascript - 尝试更新 Meteor.users 结果为 "Access Denied",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34085438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com