0

I know this question might be a little basic but I think I am lacking some important fundamental concept. I am coding in node.js, and I have a function getPostInfo(). In the end, I have a return statement that returns the object that I created. However, when I run it on PowerShell I get no output. Furthermore, when I console.log(object) I get the required answer.

If someone knows what I might be doing wrong, let me know.

P.S. - The major chunks of code in the middle can be skipped as they are just to get information of a webpage


const cheerio = require('cheerio');
const axios = require('axios');

let object = {};

const getPostInfo = async () => {
  const {data} = await axios.get('https://www.imdb.com/search/title/?groups=top_1000&ref_=adv_prv');
  // console.log(data)

  const $ = cheerio.load(data);

  const titles = [];
  const date = [];
  const runtime = [];
  const rating = [];
  const metascore = [];
  const votes = [];
  const grossEarning = [];

  $('h3 a').each((i, el) => {
    titles[i] = $(el).text().trim();
  })

  $('h3 .lister-item-year').each((i, el) => {
    date[i] = $(el).text();
  })

  $('.runtime').each((i, el) => {
    runtime[i] = $(el).text().trim();
  });

  $('.ratings-imdb-rating').each((i, el) => {
    rating[i] = $(el).text().trim();
  })

  $('.ratings-bar').each((i, el) => {
    if ($(el).find('.ratings-metascore .favorable').length > 0) {
      metascore[i] = $(el).find('.ratings-metascore .favorable').text().trim();
    }

    if ($(el).find('.ratings-metascore .mixed').length > 0) {
      metascore[i] = $(el).find('.ratings-metascore .mixed').text().trim();
    }
  })

  const nv = [];

  $('.sort-num_votes-visible').each((i, el) => {
    if($(el).find('span')){
      // text-muted has text 'votes:', however we need the number of votes which is in next() span tag
      nv[i] = $(el).find('.text-muted').next().text();
      votes[i] = nv[i].split('$')[0];
      grossEarning[i] = '$' + nv[i].split('$')[1];
    }
  })

  for (let i = 0; i < 50; i++) {
    object[i] = {
      title: titles[i],
      date: date[i],
      runtime: runtime[i],
      rating: rating[i],
      metascore: metascore[i],
      votes: votes[i],
      grossEarning: grossEarning[i]
    };
  }

  // This does not work but console.log(object) gives me a list of objects
  return object
  // console.log(object);
}

getPostInfo()

TanDev
  • 359
  • 3
  • 5
  • 13
  • 1
    you called getPostInfo()? did you console.log(getPostInfo()) that? what does it say? – Abu Sufian Aug 14 '20 at 18:04
  • yes the last line in the code block is the getPostInfo(). I did a console.log(object) after the for loop in the method and just called the function – TanDev Aug 14 '20 at 18:05
  • As @AbuSufian has mentioned try console.log(getPostInfo()); – Sushan Sapaliga Aug 14 '20 at 18:07
  • Doing that prints out this, Promise {} – TanDev Aug 14 '20 at 18:07
  • 1
    getPostInfo returns a promise. try `getPostInfo().then(obj => console.log(obj))`. for example, `async function() { return 42 }` is a function that returns `Promise<42>`, not `42`. that's what `async` does. you'll want to read up on promises in general as a beginner. – danneu Aug 14 '20 at 18:08
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Heretic Monkey Aug 14 '20 at 18:12
  • getPostInfo().then(obj => console.log(obj)). This was the solution that worked for me. I think I have to practice promises but can someone elaborate on this. Thanks @danneu – TanDev Aug 14 '20 at 18:14
  • 1
    a promise is a "box" that will eventually resolve with a value or fail with an error. your function returns a promise immediately, but it won't have a value until the request to imdb finishes (could take a while). in the meanwhile, you can chain transformations onto the box with .then() for when the value does arrive. `axios.get` also returns a promise, and inside an `async` function you can use `value = await somePromise` to wait and unpack the value. outside of `async` fn you must use `then`. of course, SO comments aren't the place for a lesson in javascript basics, lol. good luck. – danneu Aug 14 '20 at 18:39

0 Answers0