J
J
JIakki2015-11-20 00:09:05
JavaScript
JIakki, 2015-11-20 00:09:05

How not to repeat the same action in methods?

There is a constructor, and all prototype methods start the same way:

function DB() {
}

DB.prototype.add = function (req, res, next) {
    Token.findOne({token: req.token}, function(err, token) {
    .....
    .....
    })

}
DB.prototype.addToSource =  function (req, res, next) {
    Token.findOne({token: req.token}, function(err, token) {
    .....
    .....
    })
}

// остальние методы

module.exports = new DB;

How not to repeat the same action?
Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Ernest Faizullin, 2015-11-20
@JIakki

I think that approximately in this direction to act. Maybe missed something

function DB() {
  this.token = function(req) {
    return Token.findOne({token: req.token}, function(err, token) {
      .....
      .....
    })
  }
}

DB.prototype.add = function (req, res, next) {
   var token =  this.token(req);
}
DB.prototype.addToSource =  function (req, res, next) {
   var token =  this.token(req);
}

// остальние методы

module.exports = new DB;

K
Konstantin Kitmanov, 2015-11-20
@k12th

You can try AOP .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question