1

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();
Darshan
  • 102
  • 1
  • 14
  • Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) –  Jan 26 '22 at 10:00
  • Putting a `return` inside a callback does *not* return from the outer function. You need to use `return unirest.get(...).....then(return data in here);` and now you can `await` that. Note that your `data` function doesn't need the `async` keyword since it only returns a promise and doesn't await anything. –  Jan 26 '22 at 10:03
  • should I also use await data() in the hello function or call it as data() only but the Hello function is async – Darshan Jan 26 '22 at 10:08
  • The rest is fine. If you did `result2 = data()` then you'd store the Promise in `result2`, not the actual data it resolves to. –  Jan 26 '22 at 10:11
  • I followed as you said, I added return unirest.get() then return description . Then I await data() storing it in result2 array but it gave me an error " throw new AssertionError(obj)" – Darshan Jan 26 '22 at 10:20
  • Use this: https://pastebin.com/Dk97AQyF –  Jan 26 '22 at 10:28
  • Can you tell me how I can return multiple things from unirest block, I tried returning an array of responses like return[ 1st thing, 2nd things ] and accessing them in my Hello function as result[0] and result[1] , but it did't work . – Darshan Jan 26 '22 at 11:57
  • Just create an object and return that, then use destructuring: https://pastebin.com/6si2gXv1 –  Jan 26 '22 at 12:14

1 Answers1

0

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();
ggorlen
  • 44,755
  • 7
  • 76
  • 106