Cheerio错误处理程序Node JS



我使用node.js启动一个新项目,我想知道是否有任何方法可以管理Cheerio.load()函数的返回的内容。我尝试使用回调和承诺(然后抓住),但它不起作用。

示例:

 var $ = cheerio.load(html);
 //what if it's falied ???? how I can handle it?

我之所以问这个,是因为我尝试运行脚本服务时间,但有时是工作,有时不工作。

btw:我正在使用NPM站点的Cheerio模块 -> npm install cheerio.

谢谢: - )

您可以使用try/catch块,因为您不知道Cheerio操作是否会成功。

const cheerio = require('cheerio')
try {
  const $ = cheerio.load(html)
} catch (e) {
  console.log(e) // handle error
}
console.log('continue script')

最新更新