为沙发编写验证函数:
function(newDoc, oldDoc, userCtx) {
function isNullOrWhiteSpace(str){ return str === null || str.match(/^\s*$/) !== null; }
function require(field, message) {
message = message || "Document must have a " + field;
if ((!newDoc[field]) || isNullOrWhiteSpace(newDoc[field])) throw({forbidden : message});
};
require("title");
require("content");
}
一切都很好,只是我现在无法删除文档 - 验证不允许我这样做,说文档必须包含 title
和 id
。
如何检测现在正在进行哪个操作?
最佳答案
解决方案确实像沙发一样:
function(newDoc, oldDoc, userCtx) {
function isNullOrWhiteSpace(str){ return str === null || str.match(/^\s*$/) !== null; }
function require(field, message) {
message = message || "Document must have a " + field;
if ((!newDoc[field]) || isNullOrWhiteSpace(newDoc[field])) throw({forbidden : message});
};
if(!newDoc._deleted){
require("title");
require("content");
}
}
关键点是检查新文档中的 _deleted
字段。如果存在,记录删除过程。这很有趣,但很奇怪——那么,为了进行更新检查,我必须处理修订版本更改吗?但在主要方面,它是有效的。
关于javascript - CouchDB 验证可防止 DELETE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34221859/