curlcouchdbrhel7design-view

Unable to add a view to my existing database in couchDB


I have an existing design document. I want to import this design view using curl POST command. so that I can view this in fauxton.

{
    "total_rows": 1,
    "offset": 1,
    "rows": [
        {
            "id": "_design/editor",
            "key": "_design/editor",
            "value": {
                "rev": "1-c74604129e122055f0b58760a7e08ed8"
            },
            "doc": {
                "_id": "_design/editor",
                "_rev": "1-c74604129e122055f0b58760a7e08ed8",
                "language": "javascript",
                "views": {
                    "all_vendors": {
                        "map": "function(doc) {\n  if (doc.type==\"vendor\"){\n  emit(doc.name, doc.name);\n }\n}"
                    }
                }
            }
        }
    ]
}

I am trying to add this document in existing db with below command

curl -X POST http://127.0.0.1:5984/${db_name}/_design/${design_name}/_view/${view_name}

Solution

  • You're not showing the actual doc you're trying to upload, and you're using a POST, so I'll have to guess a bit what you're doing. You're saying the doc you're wanting to upload does not exist in the database, so let's use a PUT instead, and remove any revs that may reside in the document itself:

    % cat ddoc.json 
    {"_id":"_design/editor","views":{"all_vendors":{"map":"function (doc) {\n  if (doc.type==\"vendor\"){\n    emit(doc.name, doc.name);\n  }\n}"}},"language":"javascript"}
    

    So there's your view: note that there is no _rev field. This is crucial. Let's curl that to the database (I'm using Cloudant, but the same thing should work for couchdb):

    % acurl -XPUT 'https://skruger.cloudant.com/source/_design/editor' -d@ddoc.json
    {"ok":true,"id":"_design/editor","rev":"3-42790f55c52a203d1e83e0e94c2664a0"}
    

    I can now see that view in fauxton just fine:

    Fauxton showing uploaded ddoc view

    and the named view itself:

    enter image description here