简体   繁体   English

javascript 无法将 undefined 或 null 转换为 object 问题

[英]javascript cannot convert undefined or null to object question

I am trying javascript for the first time and I am having this trouble with the example:我第一次尝试 javascript 并且我在示例中遇到了这个问题:

https://www.twilio.com/blog/web-scraping-and-parsing-html-with-node-js-and-cheerio https://www.twilio.com/blog/web-scraping-and-parsing-html-with-node-js-and-cheerio

It is a web scrapper example that uses got and cheerio, both of which I have installed.这是一个使用 got 和cheerio 的 web 抓取示例,我已经安装了这两个示例。 But when i run the sample code it gives me 'cannot convert undefined or null to object error.但是当我运行示例代码时,它给了我“无法将未定义或 null 转换为 object 错误。

Why is that?这是为什么? I didn't change anything from the example at all.我根本没有改变示例中的任何内容。

the code in question:有问题的代码:

  const $ = cheerio.load(response.body);

  $('a').each((i, link) => {
    const href = link.attribs.href;
    console.log(href);
  });
}).catch(err => {
  console.log(err);
});```

How does your index.js file look like?您的 index.js 文件看起来如何? I did the tutorial and my code is working.我完成了教程,我的代码正在运行。 Maybe you are miss typed the url?也许您错过了输入 url?

Here is my index.js这是我的 index.js

const fs = require("fs");
const cheerio = require("cheerio");
const got = require("got");

const vgmUrl = "https://www.vgmusic.com/music/console/nintendo/nes";

got(vgmUrl)
  .then((response) => {
    const $ = cheerio.load(response.body);
    $("a").each((i, link) => {
      const href = link.attribs.href;
      console.log(href);
    });
  })
  .catch((err) => {
    console.log(err);
  });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM