2

I am building a reddit API wrapper in node.js to become more familiar with js and node. Here is my code so far:

./lib/reddit.js:

var request = require("request");

var reddit = function () {

  var self = this,
  userAgent = "node.js api wrapper",
  debug = false,
  uh = "",
  cookie = "";

  self.getJSON = function (url, callback) {
    request(url, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        callback(body);
      }
    });
  };

  self.post = function (url, data, callback) {
    request.post(url, { form: data }, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        callback(body);
      }
    });
  };

};

reddit.prototype = {

  login: function (username, password) {
    var data = {
      "user": username,
      "password": password,
      "rem": true
    };

    this.post("http://www.reddit.com/api/login", data, function (body) {
      var response = JSON.parse(body);
      this.uh = response.json.data.modhash;
      this.cookie = response.json.data.cookie;
      console.log(response);
      console.log(this.uh);
      console.log(this.cookie);
    });
  }

}

exports.reddit = reddit;

app.js:

var reddit = require("./lib.reddit").reddit;
reddit.login("username", "password");

I get this error:

[jet@cowboybebop reddit]$ node app.js 

/home/jet/projects/reddit/app.js:5
reddit.login("username", "password");
   ^
TypeError: Object function () {

  var self = this,
      userAgent = "node.js api wrapper",
      debug = false,
      uh = "",
      cookie = "";

  self.getJSON = function (url, callback) {
    request(url, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        callback(body);
      }
    });
  };

  self.post = function (url, data, callback) {
    request.post(url, { form: data }, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        callback(body);
      }
    });
  };

} has no method 'login'
at Object.<anonymous> (/home/jet/projects/reddit/app.js:5:8)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)

I originally had the login function defined like

reddit.prototype.login = function () .....

but this doesn't work either. Another SO question recommended formatting it like I have it now, but it still doesn't recognize it.

1
  • I figured it was something really simple. If you add this as an answer I'll accept it for you. Commented Dec 16, 2012 at 20:49

3 Answers 3

4
var reddit = require("./lib.reddit").reddit;

var a = new reddit();

a.login("username", "password");
Sign up to request clarification or add additional context in comments.

1 Comment

You barely beat the other guys! Thanks everyone!
0

Changed my mind about this problem. It's not that get is reserved, rather that you are trying to run a method in a function you haven't instantiated by mixing two styles of object definition (as a function and as a prototype). self.post won't exist until you actually instantiate a reddit object.

I think the best thing would be either to move your self.post function definition to a prototype so you can use it as a static method, or to move your login function definition within the function definition and actually instantiate the object to use its' methods.

Comments

0

This construct:

var reddit = function () { };

is just defining a function, not a class. You need the new operator to invoke it and create an object. You probably want to use this:

var reddit = new function () { };

which calls the function and creates an object of your (anonymous) class. The rest of your code should work as is it!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.