javascript - 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();
})
}
我试过了:
- 将其全部包装在 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
推荐阅读
- kubernetes - Kubernetes 仪表板不可用
- vue.js - vuejs SPA 应用程序和预渲染指南(+ 站点地图)
- android - 小吃店的自定义幻灯片动画
- java - pdfbox 以什么顺序解析 pdf 文件?(假设一页多于 1 列)
- css - 如何在此 div 容器中居中此文本
- android - 如何从appium android中的下拉TextBox中查找元素
- mysql - How can I check if the same ID has different values in different rows?
- javascript - 如果我们想将数据从一个组件传递到另一个组件,如何使用 react-compose?
- php - 在 Chart.JS 中显示 SQL 数据
- c++ - 在类定义和静态数据使用之外定义私有成员函数