简体   繁体   中英

Cannot read properties of undefined (reading 'statusCode') Nodejs using cheerio

am trying to get the title of a webpage for this amusing request and cheerio there am facing this issue Cannot read properties of undefined (reading 'statusCode') but when I run to try to run separately with the static value it works

can anyone please help to identify the miskate I have done in my code?

here is my code

var request = require("request");
var cheerio = require("cheerio");

jsonData = [
  { Domain: "bar-n-ranch.com" },
  { Domain: "barcelona-enabled.com" },
  { Domain: "barefootamelia.com" },
  { Domain: "barmranch.com" },
  { Domain: "barnstablepatriot.com" },
  { Domain: "barrieapartmentrentalsonline.com" },
  { Domain: "basquehomes.com" },
  { Domain: "bassmaster.com" },
  { Domain: "basswoodresort.com" },
];

function fetchTitle(url, onComplete = null) {
  request(url, function (error, response, body) {
    var output = url; // default to URL

    if (!error && response.statusCode === 200) {
      var $ = cheerio.load(body);
      console.log(`URL = ${url}`);

      var title = $("head > title").text().trim();
      console.log(`Title = ${title}`);

      output = `[${title}] (${url})`;
    } else {
      console.log(`Error = ${error}, code = ${response.statusCode}`);
    }

    console.log(`output = ${output} \n\n`);

    if (onComplete) onComplete(output);
  });
}

jsonData.forEach(function (table) {
  var tableName = table.Domain;
  var URL = "http://" + tableName;
  fetchTitle(URL);
});

when am passing a value like fetchtitle("https://www.googlecom") it works but am getting error when I try to loop JSON data

error stack trace ^

TypeError: Cannot read properties of undefined (reading 'statusCode')
    at Request._callback (C:\1\naveen\Project\Final\scrap example\test.js:29:56)
    at self.callback (C:\1\naveen\Project\Final\scrap example\node_modules\request\request.js:185:22)
    at Request.emit (node:events:390:28)
    at Request.onRequestError (C:\1\naveen\Project\Final\scrap example\node_modules\request\request.js:877:8)
    at ClientRequest.emit (node:events:390:28)
    at Socket.socketErrorListener (node:_http_client:447:9)
    at Socket.emit (node:events:390:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)

Thank you very much in advance

Sometimes when you loop through multiple request on server, there are chances that socket may get hanged up (busy on some other request) or server can't queue the request which can result into no response from request. Best solution to this is, you must check response object from request first before accessing properties on it.

    if (!error && (response && response.statusCode) === 200) {
        var $ = cheerio.load(body);
        console.log(`URL = ${url}`);

        var title = $("head > title").text().trim();
        console.log(`Title = ${title}`);

        output = `[${title}] (${url})`;
    }
    else {
        console.log(`Error = ${error}, code = ${response && response.statusCode}`);
    }

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