Darshan
Darshan

Reputation: 122

Getting null response from cheerio block

I am trying to get descriptions array data in another array of Hello function but I get an error, "Cannot read property length of undefined", while I already consoled the description array and it is giving me the needed data. Then what might be the reason for this error .

const unirest = require("unirest");
const cheerio = require("cheerio");

const data = async () => {
  var description = [];
  unirest
    .get("https://www.google.com/search?q=rotating proxies")
    .headers({ Accept: "application/json", "Content-Type": "application/json" })
    .proxy(
      "proxy"
    )//hided
    .then((response) => {
      const $ = cheerio.load(response.body);

      $(".uEierd").each((i, el) => {
        description[i] = $(el).find(".yDYNvb").text();
        console.log(description[i]);
        return description;
      });
    });
};
async function Hello() {
  var result2 = [];
  result2 = await data();
  for (let i = 0; i < result2.length; i++) {
    console.log(result2[i]);
  }
}
Hello();

Upvotes: 1

Views: 224

Answers (1)

ggorlen
ggorlen

Reputation: 57445

Two issues:

  • failing to return the unirest promise chain from your data() function
  • returning description in the wrong place (.each ignores the return value; .then() is where you want to return from)

Here's a possible fix:

const cheerio = require("cheerio");
const unirest = require("unirest");

const data = () => {
  return unirest
    .get("https://www.google.com/search?q=rotating proxies")
    .headers({
      Accept: "application/json",
      "Content-Type": "application/json",
    })
    .proxy("proxy")
    .then(response => {
      const $ = cheerio.load(response.body);
      const descriptions = [];

      $(".uEierd").each((i, el) => {
        descriptions.push($(el).find(".yDYNvb").text());
      });

      return descriptions;
    });
};

const hello = async () => {
  const results = await data();
  console.log(results);
};
hello();

Upvotes: 0

Related Questions