【问题标题】:AWS Lambda Function: Cannot read property 'logs' 'numberOfPages' 'stream' of undefined. Package issue or AWS Lambda issue?AWS Lambda 函数:无法读取未定义的属性“logs”“numberOfPages”“stream”。包问题或 AWS Lambda 问题?
【发布时间】: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


【解决方案1】:

更新!!!

我尝试更改任何我想要的名称,它工作正常。如果我删除pdf,它将无法正常工作。与上一个错误相同的问题。这里到底发生了什么?

const fs = require("fs");
var AWS = require("aws-sdk");
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
  function(err, haha) {
    const output = fs.createWriteStream("output.pdf");
    console.log("Process");
    //console.log(test.logs);
    //console.log(test.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.
    haha.stream.pipe(output);
    console.log("Done");
    callback(null, "done");
  }
);
console.log("End");
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 2019-05-01
    • 2017-08-22
    • 1970-01-01
    • 2021-12-08
    • 2019-07-22
    相关资源
    最近更新 更多