Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save array from cheerio in node.js

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.

like image 944
bogdanioanliviu Avatar asked Aug 16 '14 20:08

bogdanioanliviu


1 Answers

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]);}
})
like image 117
Catalyst Avatar answered Sep 23 '22 19:09

Catalyst