I have a simple code in Node js using cherio and requests. The problem is pretty intresting i dont have any error but the problems is i iterating overs the Links and i am appending the Links in a Stack or a array when i write return Function i am not getting my data back
I have also added Comments so you can read when i am returning the data when i print it i am getting undefined can someone please help
/*
Author: Soumil Shah
Website: http://soumilshah.herokuapp.com
*/
try{
const cherio = require('cherio');
const request = require('request');
const fs = require('fs')
console.log('All Modules Loaded ..... ')
}catch(e){
console.log(`Some Packages are missing ${e}`);
}
class Stack{
constructor(){
this.data = []
}
Add(elem){
this.data.push(elem);
}
Size(){
if (this.data.length == 0){return 0;}else{
return this.data.length;
}
}
Pop(){
if (this.data.length == 0){
return -1;
}else{
return this.data.pop()
}
}
isEmpty(){
if(this.data.length == 0){
return true;}
else{
return false;}
}
getStack(){
return this.data;
}
}
class ImageHunter{
constructor(WebsiteUrl = '',BaseUrl = ''){
this.URL = WebsiteUrl; // Website
this._Counter = 0; // Counter for creating File Name Image1.png etc
this.stack = new Stack(); // Stack Data Structure
this.BaseUrl = BaseUrl; // base URl
this.options = {
'method': 'GET',
'url': `${this.URL}`,
'headers': {
}
};
}
Load()
{
const request = require('request');
const cherio = require('cherio');
request(this.options, (err,resp,html)=>
{
if(!err && resp.statusCode ==200)
{
console.log(`Request was Success ${resp.statusCode}`) // success Response
const $ = cherio.load(html); // create a chery Object
$("img").each((index, image)=>{
this.counter = this.counter + 1; // Count of Images
var FileName = "Image"+ this.counter.toString() + '.jpg' // createws FileName
var img = $(image).attr('src'); // find the src
var baseUrl = this.BaseUrl // get Base URl for Images
var Links = baseUrl + img;
if (Links.toString().search(".jpg") > 0){
this.stack.Add(Links);
}
})
}else{
console.log("Error ")
}
// This Works HERE ---- - - - -- -- - - -
console.log(`Stack Size ${this.stack.Size()}`);
// Return Dont WORK
return this.stack.getStack();
})
}
}
var elem;
var image = new ImageHunter(WebsiteUrl='https://www.bridgeport.edu',BaseUrl='https://www.bridgeport.edu');
var data = image.Load();
console.log(data);