javascript - TesserActJS错误: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of null

标签 javascript node.js error-handling

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

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of null



我知道UnhandledPromiseRejectionWarning是一个 promise ,会引发错误并且没有.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();
                })

}

我试过了:
  • 将所有内容包装在try catch块中
  • 在代码
  • 中的.catch()方法之后链接.then()方法

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

    最佳答案

        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
    

    关于javascript - TesserActJS错误: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58038433/

    相关文章:

    javascript - 为什么这个正则表达式在 Javascript 中不起作用?

    javascript - 在 css/html 响应式中创建菜单栏

    javascript - react onClick 键码未定义?

    node.js - 如何使用 sequelize(nodejs) 从第 10 行到第 15 行从数据库中获取数据

    mysql - 关系型 Json 数据库?

    php - 使用PHP在MVC View 中显示自定义错误消息

    php - 在 php 中运行 javascript

    node.js - 自增运算符加二

    ajax - 代码错误导致 admin-ajax.php 500 错误

    python - 如何根据程序中的用户输入给出错误消息?