Exploring the Request in a CouchDB Show Function

Exploring the Request in a CouchDB Show Function

CouchDB is very powerful, NoSQL database that uses JSON for Document, JS for Map/Reduce queries and HTTP for an API. Since all communication with CouchDB is done via HTTP requests, CouchDB can easily act as a webserver serving data as HTML pages. The CouchDB show function is responsible for taking a document and rendering it in different formats. I’ve used CouchDB to render documents as p2 repositories, HTML pages and simple text.

function(doc, req) {
  // Render the doc is some interesting way
}

When I first started working with CouchDB I found it diffiult to understand what data was being passed around. While the req is simply a http request object, it wasn’t clear which fields were available, especially if I had a typo in my query parameter. I used a small debug trick to help me get started with CouchDB Show functions

function(doc, req) {
  if (debug) {
    return {
      "code": 200,
      "body": JSON.stringify(req)
    };
  }
}

When I set debug to true, CouchDB will render the results of the query string. This made it very easy to debug problems with my query string and understand exactly what data was available to use in the Show Function.

No Comments

Sorry, the comment form is closed at this time.