【问题标题】:TesserActJS error: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of nullTesserActJS 错误:UnhandledPromiseRejectionWarning:TypeError:无法读取属性'send' of null
【发布时间】:2020-01-26 03:26:56
【问题描述】:

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

UnhandledPromiseRejectionWarning: TypeError: Cannot read property '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. 在代码中的.then() 方法之后链接.catch() 方法

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

【问题讨论】:

    标签: javascript node.js error-handling


    【解决方案1】:
        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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-27
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多