设计文档中的变量

Variables inside design documents

为什么不能编译?

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

它抛出:

{
    "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;      }    }  })"
}

尝试通过向其添加新键来将其扩展到 newDoc 确实像下面这样也不起作用并抛出相同的错误,但只有在您尝试使用它时才会抛出,而不是在您仅声明它时抛出。

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

您正在尝试使用 CouchDB JS 引擎不支持的语法。

你应该删除 let 变量声明并使用 var

CouchDB 依赖于支持 ECMAScript Edition 5 的 SpiderMonkey 1.8.5