函数在本地工作,但在 AWS Lambda 上不工作

Function works locally but not on AWS Lambda

我正在使用 jsreport 渲染 HTML 并生成一个 PDF 文件,它在本地工作,但在 Lambda 上,它抛出这个错误:

{
  "errorMessage": "Error during rendering report: Cannot read property 'filter' of undefined",
  "errorType": "TypeError",
  "stackTrace": [
    "Phantom.execute (/var/task/node_modules/jsreport-phantom-pdf/lib/phantom.js:169:53)",
    "/var/task/node_modules/jsreport-core/lib/render/render.js:118:23",
    "tryCatcher (/var/task/node_modules/bluebird/js/release/util.js:16:23)",
    "Promise._settlePromiseFromHandler (/var/task/node_modules/bluebird/js/release/promise.js:512:31)",
    "Promise._settlePromise (/var/task/node_modules/bluebird/js/release/promise.js:569:18)",
    "Promise._settlePromise0 (/var/task/node_modules/bluebird/js/release/promise.js:614:10)",
    "Promise._settlePromises (/var/task/node_modules/bluebird/js/release/promise.js:693:18)",
    "Async._drainQueue (/var/task/node_modules/bluebird/js/release/async.js:133:16)",
    "Async._drainQueues (/var/task/node_modules/bluebird/js/release/async.js:143:10)",
    "Immediate.Async.drainQueues (/var/task/node_modules/bluebird/js/release/async.js:17:14)",
    "runCallback (timers.js:672:20)",
    "tryOnImmediate (timers.js:645:5)",
    "processImmediate [as _immediateCallback] (timers.js:617:5)"
  ]
}

相同的环境变量,相同的 Node 版本。

这是生成上述错误的行:https://github.com/jsreport/jsreport-phantom-pdf/blob/ad8d42e640348abffe77f2fed818528bee3eed98/lib/phantom.js#L169

var phantom = this.definition.options.phantoms.filter(function (p) {
  return p.version === request.template.phantom.phantomjsVersion
})

这意味着对象路径 options.phantoms 未定义。

您必须检查 definitions 在您的 Lambda 中的设置方式。

原来是这个库的问题:node-app-root-path.

我通过将正确的 rootDirectory 选项传递给 jsreport 来修复它。