0

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);
Soumil Nitin Shah
  • 634
  • 2
  • 7
  • 18
  • Your `return` statement is INSIDE the callback from `request()`. That means you're just returning back into nothing. That's not the return from `Load()`. In fact, you can't return your asynchronous data directly from your function. Instead, you will have to either return a promise (that resolves to your data) or use a callback to communicate back the data. See [How do I return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323) for details. – jfriend00 Jan 07 '20 at 00:16
  • Thanks That works now :P – Soumil Nitin Shah Jan 07 '20 at 02:27

0 Answers0