如何确保某些字段包含在 CouchDB 数据库中



我正在尝试找到一种方法来确保某些内容存在于CouchDB数据库的文档中。我认为最好的方法是使用"validate_doc_update"设计文档。我过去创建了一个检查用户是否有权对数据库进行更改的代码,因此我尝试在此处包含此附加代码。但是,它在CouchDB中不被识别为可编译的代码,我不确定为什么(见下面的代码)。有谁知道我在哪里搞砸了这段代码以使其无法编译,或者是否有更好的方法来确保某些字段包含在更新的文档中?

这包含在名为"_design/BGdesign"的同一数据库中的设计文档中:

function(newDoc, oldDoc, userCtx, secObj){
if ('_admin' in userCtx.roles) return; // skip anonymous in Admin Party case
if (userCtx.roles.indexOf('testAdmin') == -1) {
    throw({forbidden: "Not Authorized"});
    }
}
if (newDoc.artistName === undefined) {
    throw({forbidden: 'Document must have a artistName'});
}
if (newDoc.currentLocation === undefined) {
    throw({forbidden: 'Document must have a currentLocation'});
}
if (newDoc.dateMade === undefined) {
    throw({forbidden: 'Document must have a dateMade.'});
}
if (newDoc.description === undefined) {
    throw({forbidden: 'Document must have a description.'});
}
if (newDoc.name === undefined) {
    throw({forbidden: 'Document must have a name.'});
}
if (newDoc.owner === undefined) {
    throw({forbidden: 'Document must have an owner.'});
}
if (newDoc.tags === undefined) {
    throw({forbidden: 'Document must have tags.'});
}
if (newDoc.uploaded === undefined) {
    throw({forbidden: 'Document must have an uploaded date.'});
}

}

不匹配的大

括号是问题的原因。如果有更有效/"适当"的方法来做我正在尝试做的事情(除了将 if 语句压缩成一个 IF 语句,就像我即将做的那样),我将不胜感激。

最新更新