0

I have a list function for CouchDB, but I can't get it into the DB because I'm constantly getting syntax erros. Here is the function:

function(head,req) {
  var headers;
  start({'headers':{'Content-Type' : 'text/csv; charset=utf-8; header=present'}});
  while(r = getRow()) {
    if(!headers) {
      headers = Object.keys(r.value);
      send('"' + headers.join('","') + '"\n');
    }
    headers.forEach(function(v,i) {
      send(String(r.value[v]).replace(/\"/g,'""').replace(/^|$/g,'"'));
      (i + 1 < headers.length) ? send(',') : send('\n');
    });
  }
}

Can anyone show me an example of this function formatted that can be inserted into CouchDB?

Kim Stebel
  • 41,826
  • 12
  • 125
  • 142
Markuz Shultz
  • 678
  • 3
  • 9
  • 22

1 Answers1

1

List functions are stored in design documents. Design documents are JSON documents, so you need to make sure they conform to the JSON standard. Since List functions are string values in JSON, you need to make sure that you:

  • Escape any double quotes in the function, so " becomes \". Where possible, you should use single quotes instead of double quotes.
  • Make sure you replace any line breaks with \n or just remove line breaks as Javascript ignores them anyway.
  • Escape any backslashes, so \ becomes \\.
Kim Stebel
  • 41,826
  • 12
  • 125
  • 142