0

I have an question about how to call a variable inside annoymous function in node.js.

const client = require('cheerio-httpcli');

const url = 'https://puppetron.now.sh/render?url=https://festa.io/events';

client.fetch(url, (err, $, res) => {
  if(err){
    console.log(err);
    return;
  }

  const firstItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(3) > div:nth-child(1)';
  const secondItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(3) > div:nth-child(2)';
  const thirdItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(3) > div:nth-child(3)';
  const fourthItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(3) > div:nth-child(4)';
  const fifthItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(4) > div:nth-child(1)';
  const sixthItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(4) > div:nth-child(2)';
  const seventhItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(4) > div:nth-child(3)';
  const eighthItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(4) > div:nth-child(4)';

  $(firstItem).each(function(post) {
    firstItemName = $(this).text()
  });
});

console.log(client.fetch.firstItem)

In this code, I want to console log the firstItemName outside of client.fetch.

First, I tried to console.log client.fetch.firstItem, but It didn't worked.

How can I call firstItemName variable outside of client.fetch?

Sihyun Kim
  • 13
  • 4
  • in the code you posted `firstitemName` will be a global variable ... so it'll be just `firstItemName` - though it won't be populated "syncrhonously" so you'll need to wait for `client.fetch` to finish before trying to access it – Jaromanda X Jan 12 '20 at 01:38
  • @JaromandaX Hi :D Thanks for your help. I tried to ```console.log(firstItemName)``` but it just returned undefined error. I want to console.log it syncrhonously. Is there actually other way to do this? Actually what I'm making is crawl that data and turn that data to api with json format. – Sihyun Kim Jan 12 '20 at 01:49
  • of course it's undefined ... the client.fetch callback hasn't run yet - you'll need to learn how to deal with asynchrony – Jaromanda X Jan 12 '20 at 02:02
  • Is there a reason why you have to define your firstItem cons inside your function? Why not just define it outside of the scope of the anonymous function. this link https://stackoverflow.com/questions/39679212/access-a-variable-inside-a-function-which-is-inside-a-function-in-javascript may provide some more details. – portatlas Jan 12 '20 at 03:03

1 Answers1

0

You should use async/await or Promises to handle async functions.

e.g in pseudo code.

const client = require('cheerio-httpcli');
const url = 'https://puppetron.now.sh/render?url=https://festa.io/events';

async function fetchItems(url) {
  client.fetch(url, (err, $, res) => {
  if(err){
    throw new Error('Something went wrong');
    return;
  }

  const firstItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(3) > div:nth-child(1)';

    $(firstItem).each(function(post) {
      firstItemName = $(this).text()
    });
  });

  return firstItem;
}


//then you use the async function and wait until its done with processing your query.
const firstItem = await fetchItems(url);
console.log(firstItem );

//If u want to log out more properties/variables then you can return an object from the 
function with mapped variables.
Rainer Plumer
  • 3,693
  • 2
  • 24
  • 42