简体   繁体   English

如何修复 UnhandledPromiseRejectionWarning:错误:读取 ETIMEDOUT 和 UnhandledPromiseRejectionWarning:错误:写入 EPROTO 错误

[英]How do I fix UnhandledPromiseRejectionWarning: Error: read ETIMEDOUT and UnhandledPromiseRejectionWarning: Error: write EPROTO errors

I am using axios and cheerio to do some web-scraping.我正在使用 axios 和cheerio 进行一些网络抓取。 I am pretty sure I am writing this for loop wrong.我很确定我写这个for循环是错误的。 Something about making it async.关于使其异步的一些事情。 But I recently started and I couldn't understand what that means.但我最近开始,我不明白这意味着什么。 I might also be totally wrong.我也可能完全错了。

for (let i = 0; i < list.length; i++) {
    axios.get(list[i].link).then((res) => {
        const $ = cheerio.load(res.data);
        var name = $('.header').find('.itemprop').text()
        $('.knownfor-title').each((i, el) => {
            var movie = $(el).find('img').attr('src')
            var title = $(el).find('img').attr('title')
            var link = 'https://www.imdb.com' + $(el).find(".knownfor-title-role").find('a').attr('href')

            var data = {
                name,
                movie,
                title,
                link
            }
            writeStream.write(`${name}, ${movie}, ${title}, ${link} \n` ); 
            console.log(data)
        })  
    })
}

And here is the errors I get这是我得到的错误

(node:5293) UnhandledPromiseRejectionWarning: Error: read ECONNRESET
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:205:27)


(node:5293) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 512)


(node:5293) UnhandledPromiseRejectionWarning: Error: read ETIMEDOUT
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:205:27)
(node:5293) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 420)

You get the point你明白了

This should be the syntax of your axios with.then() and.catch()这应该是您的 axios 的语法 with.then() and.catch()

const axios = require('axios');

// Make a request for a user with a given ID
axios.get('/user?ID=12345')
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .then(function () {
    // always executed
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该如何解决? UnhandledPromiseRejectionWarning - how do i fix this? UnhandledPromiseRejectionWarning 我如何修复节点 UnhandledPromiseRejectionWarning - How do i fix the node UnhandledPromiseRejectionWarning 我该如何修复(节点:5796)UnhandledPromiseRejectionWarning:错误[ERR_HTTP_HEADERS_SENT]:错误? - How can i fix (node:5796) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: error? 如何仅捕获UnhandledPromiseRejectionWarning错误 - How to catch UnhandledPromiseRejectionWarning error only 我有这个错误: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'buffer' of undefined - I have this error: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'buffer' of undefined 如何修复控制台中的“UnhandledPromiseRejectionWarning” - How to fix 'UnhandledPromiseRejectionWarning' in console UnhandledPromiseRejectionWarning,如何解决? - UnhandledPromiseRejectionWarning, how to fix it? Javascript 中的 UnhandledPromiseRejectionWarning 错误 - UnhandledPromiseRejectionWarning error in Javascript Expess 中的 UnhandledPromiseRejectionWarning 错误 - UnhandledPromiseRejectionWarning error in Expess UnhandledPromiseRejectionWarning:错误:评估失败 - UnhandledPromiseRejectionWarning: Error: Evaluation failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM