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

时间:2016-02-13 13:39:39

标签: javascript couchdb design-documents

我试图找到一种方法来确保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.'});
}

}

1 个答案:

答案 0 :(得分:0)

不匹配的花括号是导致问题的原因。如果有更高效/适当的'做我想做的事情(没有像我要做的那样将if语句压缩成单个IF语句)我会非常感谢这些信息。