简体   繁体   English

无法使用cheerio读取未定义(读取'statusCode')Nodejs的属性

[英]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我试图为这个有趣的请求获取网页的标题,cheerio 面临这个问题无法读取未定义的属性(读取'statusCode')但是当我运行尝试使用 static 值单独运行时它可以工作

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当传递像 fetchtitle("https://www.googlecom") 这样的值时,它可以工作,但是当我尝试循环 JSON 数据时出现错误

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.对此的最佳解决方案是,在访问其属性之前,您必须首先检查来自请求的响应 object。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在nodejs中读取未定义的属性(读取“长度”) - Cannot read properties of undefined (reading 'length') in nodejs 无法读取未定义的属性(读取 'then') - Cannot read properties of undefined (reading 'then') 无法读取未定义的属性(读取“4”) - Cannot read properties of undefined (reading '4') 无法读取未定义的属性(读取“______”) - Cannot read properties of undefined (reading '______') 无法读取未定义的属性(读取“打开”) - Cannot read properties of undefined (reading 'on') 无法读取未定义的属性(读取 *) - Cannot read properties of undefined (reading *) 从后端nodejs读取用户时无法读取未定义的属性(读取'_id') - Cannot read properties of undefined (reading '_id') while reading user from backend nodejs 当我向我显示此错误“无法读取未定义的属性(读取'_internalPath')”时,我尝试使用nodejs和firestore中的过滤器获取数据 - I tried to get data using filter in nodejs and firestore when show me this error "Cannot read properties of undefined (reading '_internalPath')" 未捕获的类型错误:无法读取未定义的属性(读取 'forEach')(使用 Parcel ) - Uncaught TypeError: Cannot read properties of undefined (reading 'forEach') (using Parcel ) 无法使用 formik 读取未定义的属性(读取“对象”) - Cannot read properties of undefined (reading 'object') using formik
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM