Sunday, 12 March 2017

ES6 Class Inheritance returns undefined when used

So, I have a class called API which has one method fetchStatus(url). I then have another class which extends the API called MovieAPI that is being used in the FetchMovie command. So when I try to do this._Movie.fetchStatus(args.join(" ")) it comes up undefined.

API Class

/* jshint esversion: 6 */
const request = require('request');
class API {
  /**
   * @param String apiKey  the api key for whatever api your using
   */
  constructor(apiKey) {
    this.key = apiKey;
  }
  /**
   * Gets the status of server
   * @param String url   api url
   * @return Promise     true if server is up and false if not
   */
  fetchStatus(url) {
    request(url, function(error, response, body) {
      return body;
    });
  }

}

module.exports = API;

MovieDB Class

/* jshint esversion: 6 */
const API = require('./Api');
class MovieDb extends API {
  /**
   * @param String apiKey
   * @param string url
   */
  constructor(apiKey, url) {
    super(apiKey = '');
    this.url = url;
  }

  /**
   * Grab a movie from the title and year
   * @param String title   movie title
   * @param String year    movie year
   */
  fetchMovie(title, year) {
    console.log(this.url);
    console.log(title);
    console.log(year);
  }
}

module.exports = MovieDb;

FetchMovie Class

/* jshint esversion: 6 */
const configuration = require('../../configuration/settings');
const MovieDb = require('../utils/MovieApi');
class FetchMovie {
  /**
    * @param Object client       the bot
    * @param Object message      the message object
    * @param String args         the arguments typed after the command
    * @param Boolean ownerOnly   command is for owner only
  */
  constructor(client, message, args) {
      this.client = client;
      this.message = message;
      this.args = args;
      this.ownerOnly = false;
      this._Movie = new MovieDb('http://www.omdbapi.com');
  }

  /**
   * @param Object client    the bot
   * @param Object message   the message object
   * @param String args      the arguments typed after the command
  */
  run(client, message, args) {
    console.log(this._Movie.fetchStatus(args.join(" ")));
  }

  /**
   * @return Object
  */
  help() {
    return {
        command: "fetchmovie",
        docs: "returns a block of movie data",
        example: configuration.app.prefix + "fetchmovie <movie title> <movie year>",
        output: "Block of movie data",
        onlyOwner: this.ownerOnly
    };
  }

  /**
   * @return Boolean   command is for the owner only
  */
  ownerOnly() {
    return this.ownerOnly;
  }
}

module.exports = FetchMovie;

So, the log in run() is returning undefined?



via Ethan

No comments:

Post a Comment