Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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}`);
    }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!