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();