首页 > 解决方案 > TesserActJS 错误:UnhandledPromiseRejectionWarning:TypeError:无法读取属性'send' of null

问题描述

使用 Tesseract NPM 包。并得到以下错误:

UnhandledPromiseRejectionWarning:TypeError:无法读取属性'send' of null

我知道 UnhandledPromiseRejectionWarning 是一个抛出错误并且没有.catch()方法的承诺。这是我的代码(也使用 npm sharp 包)

function processText() {

    sharp('./screenshot.png')
        .extract({ left: 40, top: 585, width: 405, height: 70 })
        .resize({ height: 210 })
        .toFile(`./text.png`, function (err) {
            worker
                .recognize('./text.png')
                .then(({ text }) => {
                    console.log(text) 
                    worker.terminate();
                })

}

我试过了:

  1. 将其全部包装在 try catch 块中
  2. 在代码中的.catch()方法之后链接方法.then()

这并不能解决错误。有什么建议可以消除这个错误吗?

标签: javascriptnode.jserror-handling

解决方案


    try returning sharp

    eg:
    function processText() {

        return sharp('./screenshot.png')
          ...
          ...
    }

If you are into ES6 format, check this out

const semiTransparentRedPng = await sharp({
  create: {
    width: 48,
    height: 48,
    channels: 4,
    background: { r: 255, g: 0, b: 0, alpha: 0.5 }
  }
})
  .png()
  .toBuffer();

Reference: https://www.npmjs.com/package/sharp

推荐阅读