简体   繁体   English

nodejs无法识别对象的功能

[英]nodejs not recognizing object's function

I am building a reddit API wrapper in node.js to become more familiar with js and node. 我正在node.js中构建一个reddit API包装器,以便更熟悉js和node。 Here is my code so far: 到目前为止,这是我的代码:

./lib/reddit.js: ./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: 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. 另一个SO问题建议像我现在一样格式化它,但它仍然无法识别它。

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

var a = new reddit();

a.login("username", "password");

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). 这并不是说get是保留的,而是你试图通过混合两种样式的对象定义(作为函数和作为原型)来实现你没有实例化的函数中的方法。 self.post won't exist until you actually instantiate a reddit object. 在实际实例化reddit对象之前,self.post将不存在。

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. 我认为最好的方法是将self.post函数定义移动到原型中,以便将其用作静态方法,或者在函数定义中移动登录函数定义并实际实例化对象以使用其方法。

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. 您需要new运算符来调用它并创建一个对象。 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! 你的其余代码应该像它一样工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM