Reputation: 928
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.
Upvotes: 2
Views: 2412
Reputation: 2456
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!
Upvotes: 0
Reputation: 3281
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.
Upvotes: 0
Reputation: 9095
var reddit = require("./lib.reddit").reddit;
var a = new reddit();
a.login("username", "password");
Upvotes: 4