Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

294
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda