gpt4 book ai didi

mongodb - 允许规则更新 meteor mongodb

转载 作者:可可西里 更新时间:2023-11-01 10:19:58 26 4
gpt4 key购买 nike

浏览器控制台显示:

update failed: Access denied

下面的代码应该允许客户端将 value 数组的值设置为 undefined 但它失败并出现上述错误。
知道为什么以及如何解决它吗?谢谢

//client
const doc = myCol.findOne({element: 'abc'});
myCol.update({_id: doc._id}, {$set: {value: undefined}});

//server
myCol.allow({
update: function(userId, doc, fields, modifier) {
console.log(modifier); // -----=> { '$set': {} } --------------why?
let edits = {$set: {value: undefined}};

const userVarified = (userId && doc.userId == userId);
const ABC = (doc.element === 'abc');
const XYZ = (doc.action === 'xyz' && doc.element === 'x' && doc.name === 'y' && _.difference(fields, ['values]).length === 0);
try {
if (userVarified) {
if (ABC) {
check(modifier, Match.OneOf(edits));
} else if (XYZ) {
return true;
}
}
} catch (e) {
return false;
}
return true;
}
});

编辑 1
修饰符是 { '$set': {} } 当服务器获取它时。知道为什么吗?

编辑2
根据 Mikkel 的回答,它修复了“编辑 1”中的错误。
该代码现在使用修饰符 { '$set': { value: '' } }

现在表达式 check(modifier, Match.OneOf(checkEdit)); 给出 false 但我希望它匹配,因为客户端发送的内容和 console.log(modifier)是一样的。
知道为什么它是错误的吗?

已修复
import { Match } from 'meteor/check' 添加到文件的顶部并且
使用 Match.test(modifier, checkEdit); 代替。

最佳答案

尝试将值设置为“undefined”是没有意义的,Mongo 不允许您这样做。

根据 mongo 文档,我认为您应该改用 $unset:

$unset The $unset operator deletes a particular field. Consider the following syntax:

{ $unset: { : "", ... } } The specified value in the $unset expression (i.e. "") does not impact the operation.

To specify a in an embedded document or in an array, use dot notation.

https://docs.mongodb.com/manual/reference/operator/update/unset/

关于mongodb - 允许规则更新 meteor mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40145904/

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