Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

JS Scope issue

I am trying to scrape a website, and I am storing some elements inside an array inside a callback function. I want to take it out of the function, but when i console log the array outside of the function, I get an empty array. How can I solve this issue?

var request = require('request');
var cheerio = require('cheerio');


var links=[];

var url="http://www.shirts4mike.com/shirts.php";
request(url,function(error,response,body){
  if (!error){
    var $ = cheerio.load(body);
    $('a','.products').each(function(){
      var link = ($(this).attr('href'));
            links.push("http://www.shirts4mike.com/" + link);
    });
  }
});
console.log(links)

3 Answers

Steven, actually when I try to log like this, it is showing the array with all of its elements. I just want to access it outside of the callback.

var request = require('request');
var cheerio = require('cheerio');


var links=[];

var url="http://www.shirts4mike.com/shirts.php";
request(url,function(error,response,body){
  if (!error){
    var $ = cheerio.load(body);
    $('a','.products').each(function(){
      var link = ($(this).attr('href'));
            links.push("http://www.shirts4mike.com/" + link);
    });
console.log(links)
  }
});
Steven Parker
Steven Parker
243,656 Points

But you need to be careful about when the code outside the callback is being executed. You know the code in the callback only runs after the response to the request is received. But what are you planning to do outside the callback to be sure you're not trying to use links before the response comes in?

If you try to use the array too soon, it will always be empty. This is the case when your first code example logs links immediately after making the request. But in your second example, the log is part of the callback.

Steven,

Now I understand that because the links array is global, and because it is logged first to the terminal before the callback, I get an empty array. But I still dont know how to access it outside of the callback with its newly added items inside. Should log it after 2 seconds?

Steven Parker
Steven Parker
243,656 Points

But then you're just guessing at when the response will be received. Perhaps it would help if I understood why you're needing to access this information outside the callback.

I would think that normally you would only want to use this information inside the callback, because you know that when it runs the information is available. Doing it elsewhere will require some synchronization mechanism. What is it you are doing that would require that?