3

I have a java script file that is referencing another javascript file that contains a class using

const Champion = require("./championgg_webscraper_cheerio.js");

I then try to instantiate an object of the class Champion by

var temp = new Champion("hello");
console.log(temp);

And when I do it prints this to the console indicating and undefined variable:

Champion {}

Also when i try to print out the properties of the class I get undefined, I think it might not have access to the most_frequent_completed_build variable.

console.log(temp.most_frequent_completed_build);

Here is a look at the championgg_webscraper_cheerio.js file

function Champion(champName) {
  //CHEERIO webscraping
  var cheerio = require('cheerio');
  //REQUEST http library
  var request = require('request');
  //url of the champion
  var url = "http://champion.gg/champion/Camille/Top?";
  var most_frequent_completed_build;
  var highest_win_percentage_completed_build;
  request(url,
    function(error, response, html) {
      if (!error && response.statusCode == 200) {
        var $ = cheerio.load(html);
        var final_build_items = $(".build-wrapper a");
        var mfcb = [];
        var hwpcb = [];
        for (i = 0; i < 6; i++) {
          var temp = final_build_items.get(i);
          temp = temp.attribs.href;
          //slices <'http://leagueoflegends.wikia.com/wiki/> off the href
          temp = temp.slice(38);
          mfcb.push(temp);
        }
        for (i = 6; i < 12; i++) {
          var temp = final_build_items.get(i);
          temp = temp.attribs.href;
          //slices <'http://leagueoflegends.wikia.com/wiki/> off the href
          temp = temp.slice(38);
          hwpcb.push(temp);
        }
        most_frequent_completed_build = mfcb;
        highest_win_percentage_completed_build = hwpcb;
      } else {
        console.log("Response Error: " + response.statusCode);
      }
    }
  );
};
module.exports = Champion;
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
Patrick D
  • 79
  • 8
  • Can you try `const Champion = require("./championgg_webscraper_cheerio.js").Champion;`? – Sajib Khan May 14 '18 at 05:18
  • I have modified Champion's declaration to: `const Champion = require("./championgg_webscraper_cheerio.js").Champion;` It now says Champion is not a constructor when I try: `var temp = new Champion("hello");` – Patrick D May 14 '18 at 05:25

3 Answers3

2

I think you want a Function constructor named Champion (a prototype or blue-print like classes in other programming languages like Java).

As an alternative I would suggest you to learn ES6 way of writing classes which is similar to that of Java.

You can achieve that by adding all the variables or methods to the this variable inside the Function Constructor so that you can access them using an object created using the 'new' keyword i.e make them Class members or methods.

In your case,

function Champion(champName) {
    //Some code

    this.most_frequent_completed_build = NULL;

    //Rest of code
}

module.exports = Champion;

Just make sure whenever you try to access Class variables always use this.variable_name like this.most_frequent_completed_build.

So when you create a new object of this Class in main app you will be able to access all Class members and methods.

const Champion = require("./championgg_webscraper_cheerio.js");

var temp = new Champion("hello");
console.log(temp.most_frequent_completed_build);
Vishal-L
  • 1,307
  • 1
  • 9
  • 16
0

You are exporting a function

All you have to do is call that function like

var temp = Champion();

You can read more about new keyword here and here

AbhinavD
  • 6,892
  • 5
  • 30
  • 40
  • I have changed the declaration of temp to be `var temp = Champion("hello");` and I am met with `Cannot read property most_frequent_completed_build of undefined` – Patrick D May 14 '18 at 05:33
  • Yes. It will not as the scope of `most_frequent_completed_build` is within that function. You can return this value from the function if you need it. – AbhinavD May 14 '18 at 05:39
  • From my understanding of objects in Java, if I have a class that has variables within that class, the Class object will be able to see its own variables. I am trying to make Champion work like a class that has two public arrays of `most_frequent_completed_build` and `highest_win_percentage_completed_build` that are set to the information provided from the given url. Is it possible to return a Champion object from the Champion constructor? – Patrick D May 14 '18 at 05:44
  • You cannot compare with Java, unfortunately. "this" key is totally different for example. And your code is running in async mode, so even if you reach this variable, it would still be undefined – scetiner May 14 '18 at 06:34
  • Yes I think I was recognizing that as I was debugging. Is there a way to wait for the request function to finish before we assign them back to the object. – Patrick D May 14 '18 at 07:49
  • yes, you have three choices.. `callback`, `promise` or `async/await` – AbhinavD May 14 '18 at 08:42
0
    function Champion(champName) {
  //CHEERIO webscraping
  var cheerio = require('cheerio');
  //REQUEST http library
  var request = require('request');
  //url of the champion
  var url = "http://champion.gg/champion/Camille/Top?";
  var most_frequent_completed_build;
  var highest_win_percentage_completed_build;
  request(url,
    function(error, response, html) {
      if (!error && response.statusCode == 200) {
        var $ = cheerio.load(html);
        var final_build_items = $(".build-wrapper a");
        var mfcb = [];
        var hwpcb = [];
        for (i = 0; i < 6; i++) {
          var temp = final_build_items.get(i);
          temp = temp.attribs.href;
          //slices <'http://leagueoflegends.wikia.com/wiki/> off the href
          temp = temp.slice(38);
          mfcb.push(temp);
        }
        for (i = 6; i < 12; i++) {
          var temp = final_build_items.get(i);
          temp = temp.attribs.href;
          //slices <'http://leagueoflegends.wikia.com/wiki/> off the href
          temp = temp.slice(38);
          hwpcb.push(temp);
        }
        most_frequent_completed_build = mfcb;
        highest_win_percentage_completed_build = hwpcb;
      } else {
        console.log("Response Error: " + response.statusCode);
      }
    }
  );
  return {most_frequent_completed_build:most_frequent_completed_build};
};
module.exports = Champion;

    var temp = new Champion("hello");
console.log(temp.most_frequent_completed_build);
Rahul Bisht
  • 144
  • 6