Save array from cheerio in node.js

bogdanioanliviu picture bogdanioanliviu · Aug 16, 2014 · Viewed 7.5k times · Source

I have this code and I'm not able to create a new array for further use:

var request = require("request");
var cheerio = require("cheerio");
var pag = [];
request('http://www.tastez.ro/tv.php?query=sopcast', function(error, response, body) {
  if (error) {
    return console.error('upload failed:', error);
  }

  var $ = cheerio.load(body);
    links = $(".page a"); //use your CSS selector here
    $(links).each(function(i, link){    
      var   sop = $(this).attr('href');
      pag[i] = sop;  //aici pun val gasite in locuri in array
    });
  pag.push(', '); 
});
for (var i=0; i<2; i++){
  console.log(pag[i]);
}

When I run the code it is listing undefined. But if I put the code like this:

var request = require("request");
var cheerio = require("cheerio");
var pag = [];
request('http://www.tastez.ro/tv.php?query=sopcast', function(error, response, body) {
  if (error) {
    return console.error('upload failed:', error);
  }

  var $ = cheerio.load(body);
  links = $(".page a"); //use your CSS selector here
  $(links).each(function(i, link){    
    var sop = $(this).attr('href');
    pag[i] = sop;  //aici pun val gasite in locuri in array
  });
  pag.push(', ');
  for (var i=0; i<2; i++){
    console.log(pag[i]);
  }   
});

Then it is displaying correct result but still undefined when i'd like to use it later. Can someone help me up with this.

Answer

Catalyst picture Catalyst · Aug 16, 2014

Node.js is async, that means the scrape hasn't finished yet when you go to print out the array.

I'm not totally sure what your end goal is, but here is a way to do what you are trying with minimal changes:

var request = require("request");
var cheerio = require("cheerio");
var pag = [];

var scrape = function( callback ) {
    request('http://www.tastez.ro/tv.php?query=sopcast', function(error, response, body) {
      if (error) {
        return console.error('upload failed:', error);
      }

    var $ = cheerio.load(body);
     links = $(".page a"); //use your CSS selector here
      $(links).each(function(i, link){    
        var sop = $(this).attr('href');
        pag[i] = sop;  //aici pun val gasite in locuri in array

      });
    pag.push(', '); 
    if (callback) callback()
    });
}
scrape(function() {
    for (var i=0; i<2; i++){
    console.log(pag[i]);}
})