Search code examples
javascriptnode.jsweb-scrapingcheerio

I've error with this selector when I'm using cheerio


I'm using node, express, cheerio and axios to create a simple web scraper. In this example, axios has data but after loading data with cheerio I've this error:

(node:14056) UnhandledPromiseRejectionWarning: TypeError: content.forEach is not a function

Scraper code:

app.get('/users', (req, res) => {
  axios('https://fake.com/users')
  .then(response => {
    let $ = cheerio.load(response);
    let users = $('.users .user');
    console.log(cards);
  });
});

But why has this error? because I'm passing data after resolve the promise in then block??


Solution

  • Because you used response and you have to use response.data instead:

    let $ = cheerio.load(response.data);

    Actually response returns all response has received from server, but response.data returns only data that has received from API.