简体   繁体   English

CouchDB设计文档和validate_doc_update函数遇到问题

[英]Having trouble with CouchDB design document and validate_doc_update function

I'm new to CouchDB, and I'm having a lot of trouble with getting my design document to work in my database. 我是CouchDB的新手,并且在使设计文档在数据库中工作时遇到很多麻烦。

This is my design document: 这是我的设计文件:

{
"language": "javascript",

"validate_doc_update": "function(newDoc, oldDoc, userCtx) {
    function require(field, message) {
        message = message || "Document must have a " + field;
        if (!newDoc[field]) throw({forbidden : message});
    };
    function unchanged(field) {
        if (oldDoc && toJSON(oldDoc[field] != toJSON(newDoc[field])) {
        }
    };

    if (newDoc.type == "fortune") {
        require("body");
        require("sequence_id");
        require("created_at");
        unchanged("sequence_id");
        unchanged("created_at");
    }
};",

"views": { 
    "fortune_count": {
        "map": "function(doc) { if(doc.sequence_id && doc.body) { emit(doc.sequence_id, doc.body); }",
        "reduce": "_count"
        }
},

"shows": {
}
}

Let me break down the problems I'm having: 让我分解一下我遇到的问题:

  1. I'm having trouble sending this to my database as a put request. 我在将其作为放置请求发送到我的数据库时遇到了麻烦。 Apparently this is invalid JSON. 显然,这是无效的JSON。 I'm not an expert at JSON, and this extremely frustrating. 我不是JSON方面的专家,这非常令人沮丧。 Any thoughts? 有什么想法吗?

  2. Because of the problems I was having with the put request, I decided to add my design document using Futon. 由于放置请求存在问题,因此我决定使用Futon添加设计文档。 My database seems to be fine with the view I have here, but when I try to add documents to the database, I get an error saying that my validate_doc_update text doesn't evaluate to a function. 我在这里的视图看来数据库不错,但是当我尝试向数据库中添加文档时,出现一条错误消息,提示我的validate_doc_update文本无法求值。 Any advice on what is wrong with my validate function? 关于我的validate函数有什么问题的任何建议吗?

Because of the difficulty I'm having with my validate function, I tried putting a dumb version in the design doc: 由于我的validate函数遇到困难,因此我尝试在设计文档中放入一个哑版本:

"validate_doc_update": "function(newDoc, oldDoc, userCtx) { if (newDoc.type == "fortune") {} };",

I can create documents with this version in place, but it is not of much use. 我可以使用此版本来创建文档,但是并没有太大用处。

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks, 谢谢,

Paul Nichols 保罗·尼科尔斯

您只是忘了在(oldDoc && toJSON(oldDoc[field] != toJSON(newDoc[field]))关闭括号(oldDoc && toJSON(oldDoc[field] != toJSON(newDoc[field]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM