1

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;
    }
  }
}
SomeDutchGuy
  • 2,249
  • 4
  • 16
  • 42
  • @JuanjoRodriguez I literally say's my need. I need a variable inside a design document that I can work with. Declaring variables does not seem to work so I am trying to just add a key to an existing object. – SomeDutchGuy Sep 26 '19 at 08:36
  • You can define variables inside your functions. It is not clear what your mean with "create valirables inside design documents" – Juanjo Rodriguez Sep 26 '19 at 09:02
  • @JuanjoRodriguez I updated the question for you with a smaller example. – SomeDutchGuy Sep 26 '19 at 09:27

1 Answers1

3

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

Juanjo Rodriguez
  • 2,103
  • 8
  • 19