简体   繁体   中英

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.

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. I'm not an expert at JSON, and this extremely frustrating. Any thoughts?

  2. Because of the problems I was having with the put request, I decided to add my design document using 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. Any advice on what is wrong with my validate function?

Because of the difficulty I'm having with my validate function, I tried putting a dumb version in the design doc:

"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]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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