node.js中的类方法 [英] Class methods in node.js

查看:111
本文介绍了node.js中的类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试用findOne,findOneOrCreate等方法为passport.js写一个用户模块,但是无法正确的。



User.js

  var User = function(db){
this.db = db;
}

User.prototype.findOne(email,password,fn){
//某些代码在这里
}

模块。 exports = exports = User;

app.js

 $ code User = require('./ lib / User')(db); 
User.findOne(email,pw,callback);

我经历了几十个错误,主要是

  TypeError:对象不是函数

  TypeError:Object function(){
函数用户(db){
console.log );
}
}没有方法'findOne'

如何创建一个



更新



<我曾经提出过解决方案:

  var db; 
function User(db){
this.db = db;
}
User.prototype.init = function(db){
return new User(db);
}
User.prototype.findOne = function(profile,fn){}
module.exports = User;

没有运气。

  TypeError:Object function User(db){
this.db = db;
}没有方法'init'


解决方案

有几件事情在这里,我已经纠正了你的源代码,并添加了评论来解释:



lib / User.js

  //更简洁的声明
函数用户(db){
this.db =分贝;
}

//你需要在这里分配一个新的函数
User.prototype.findOne = function(email,password,fn){
// some code这里
}

//不需要覆盖`export` ...因为你正在替换`module.exports`本身
module.exports = User;

app.js

  //不要忘记`var` 
//也不要将require作为函数调用,它是用于创建新的类声明实例
var User = require('./ lib / User');

//创建一个新的用户实例class
var user = new User(db);

//调用findOne作为一个实例方法
user.findOne(email,pw,callback);


I've been trying for the last hour to write a user module for passport.js with the findOne, findOneOrCreate, etc. methods, but can't get it right.

User.js

var User = function(db) {
  this.db = db;
}

User.prototype.findOne(email, password, fn) {
  // some code here
}

module.exports = exports = User;

app.js

User = require('./lib/User')(db);
User.findOne(email, pw, callback);

I've been through dozens of errors, mostly

TypeError: object is not a function

or

TypeError: Object function () {
  function User(db) {
    console.log(db);
  }
} has no method 'findOne'

How do I create a proper module with these functions without creating an object/instance of User?

Update

I went over the proposed solutions:

var db;
function User(db) {
  this.db = db;
}
User.prototype.init = function(db) {
  return new User(db);
}
User.prototype.findOne = function(profile, fn) {}
module.exports = User;

No luck.

TypeError: Object function User(db) {
  this.db = db;
} has no method 'init'

解决方案

A couple of things are going on here, I've corrected your source code and added comments to explain along the way:

lib/User.js

// much more concise declaration
function User(db) {
    this.db = db;
}

// You need to assign a new function here
User.prototype.findOne = function (email, password, fn) {
    // some code here
}

// no need to overwrite `exports` ... since you're replacing `module.exports` itself
module.exports = User;

app.js

// don't forget `var`
// also don't call the require as a function, it's the class "declaration" you use to create new instances
var User = require('./lib/User');

// create a new instance of the user "class"
var user = new User(db);

// call findOne as an instance method
user.findOne(email, pw, callback);

这篇关于node.js中的类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆