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} nn`);
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
Thank you very much in advance