I am writing a web scraping application. I can write this as a single script, and it will return a the array as desired. When modularizing my code, and turning it into functions, the code that should return an array returns undefined. Any pointers would be great.
"use strict";
let request = require("request");
let cheerio = require("cheerio");
function isNotUndefined(value){
if(value !==undefined){
return value;
}
}
function scrapeIndexPage(url){
var arr = new Array();
request(url, function(req,error,body){
var $ = cheerio.load(body);
var view = $("div.view-content");
view = view.html();
$ = cheerio.load(view);
for(var i=1;i<60;i++){
var getClass = ".class-row-"+i+" a";
var link = $(getClass).attr("href");
arr.push(link);
}
arr = arr.filter(isNotUndefined);
});
return arr;
}
var url = 'http://www.urlhere.com';
console.log(scrapeIndexPage(url));