【发布时间】:2020-03-10 02:56:27
【问题描述】:
我正在使用 phantom-html-to-pdf 包和 Nodejs 在 AWS Lambda 上将 html 转换为 pdf 函数。我遇到了一些错误,显示
无法读取未定义的属性“日志”
我尝试评论它,它显示另一个错误是
无法读取未定义的属性“numberOfPages”
我不确定这是包的错误还是因为它无法在 AWS Lambda 上运行。也不知道为什么 async 中的所有功能都不起作用。
代码如下:
const fs = require("fs");
var phantomjs = require("phantomjs");
const conversion = require("phantom-html-to-pdf")({});
exports.handler = (event, context, callback) => {
console.log("Start");
conversion(
{ html: "<h1>Hello World</h1>" },
// eslint-disable-next-line handle-callback-err
async (err, pdf) => {
const output = fs.createWriteStream("output.pdf");
console.log("Process");
console.log(pdf.logs);
console.log(pdf.numberOfPages);
// since pdf.stream is a node.js stream you can use it
// to save the pdf to a file (like in this example) or to
// respond an http request.
pdf.stream.pipe(output);
console.log("Done");
callback(null, "done");
}
);
console.log("End");
};
这是错误:
{
"errorType": "Runtime.UnhandledPromiseRejection",
"errorMessage": "TypeError: Cannot read property 'logs' of undefined",
"trace": [
"Runtime.UnhandledPromiseRejection: TypeError: Cannot read property 'logs' of undefined",
" at process.on (/var/runtime/index.js:37:15)",
" at process.emit (events.js:198:13)",
" at emitPromiseRejectionWarnings (internal/process/promises.js:140:18)",
" at process._tickCallback (internal/process/next_tick.js:69:34)"
]
}
{
"errorType": "Runtime.UnhandledPromiseRejection",
"errorMessage": "TypeError: Cannot read property 'numberOfPages' of undefined",
"trace": [
"Runtime.UnhandledPromiseRejection: TypeError: Cannot read property 'numberOfPages' of undefined",
" at process.on (/var/runtime/index.js:37:15)",
" at process.emit (events.js:198:13)",
" at emitPromiseRejectionWarnings (internal/process/promises.js:140:18)",
" at process._tickCallback (internal/process/next_tick.js:69:34)"
]
}
{
"errorType": "Runtime.UnhandledPromiseRejection",
"errorMessage": "TypeError: Cannot read property 'stream' of undefined",
"trace": [
"Runtime.UnhandledPromiseRejection: TypeError: Cannot read property 'stream' of undefined",
" at process.on (/var/runtime/index.js:37:15)",
" at process.emit (events.js:198:13)",
" at emitPromiseRejectionWarnings (internal/process/promises.js:140:18)",
" at process._tickCallback (internal/process/next_tick.js:69:34)"
]
}
【问题讨论】:
-
我不认为这是一个 lambda 问题。看起来
pdf为空-我认为您应该在使用pdf之前检查err。另外,我认为该函数不应该是async。 -
是的,我确实注意到 pdf 似乎为空。我稍后会在这里尝试更新。
标签: javascript node.js aws-lambda html-to-pdf