简体   繁体   中英

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. I am pretty sure I am writing this for loop wrong. 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()

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
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM