Search code examples
couchdbdesign-documents

Variables inside design documents


Why does this not compile?

function (newDoc, oldDoc, userCtx) {
  if (newDoc !== null) {
    if (newDoc.type.toLowerCase() === 'test') {
      let thisDoesNotWork = 5;
    }
  }
}

It throws:

{
    "error": "compilation_error",
    "reason": "Expression does not eval to a function. (function (newDoc, oldDoc, userCtx) {    if (newDoc !== null) {      if (newDoc.type.toLowerCase() === 'test') {        let thisDoesNotWork = 5;      }    }  })"
}

Trying to extend it to newDoc by adding a new key to it does like the follwing does not work either and throws the same error but then only when you try to use it and not if you only declare it.

function (newDoc, oldDoc, userCtx) {
  if (newDoc !== null) {
    if (newDoc.type.toLowerCase() === 'test') {
      let newDoc.thisDoesNotWork = 5;
    }
  }
}

Solution

  • You are trying to use an unsupported syntax for the CouchDB JS engine.

    You shoud remove the let variable declaration and use var

    CouchDB relies on SpiderMonkey 1.8.5 which supports ECMAScript Edition 5