iLiA
iLiA

Reputation: 3153

access parent function inside module.exports

Helo, My problem with this code is that i can not access checkIf to set its property LengthIs, and when i log if this is equal or not to module.exports, it logs false. I also logged this and it gave me some big object and i could not get what was it, maybe it was global. So what is the problem?

module.exports = {
    checkIf: function(field){
        console.log(this === module.exports)
        this.checkIf.LengthIs = function(params){
            return function(req,res,next){
        }
    }
}

Upvotes: 0

Views: 291

Answers (1)

Nikos M.
Nikos M.

Reputation: 8325

Try this (named-function expression):

module.exports = {
    checkIf: function checkIf(field){
        checkIf.LengthIs = function(params){
            return function(req,res,next){
        }
    }
}

In case you want to use the method as providing an argument and getting back custom check methods tailored to that initial argument you can try sth like the following:

module.exports = {
    checkIf: function checkIf(field){
        return {
          LengthIs: function(params){
              // use "field" parameter somewhere in this method 
              return function(req,res,next){/* whatever.. */ };
          }/*,
          .. possibly other methods as well */
        };
     }
}

Then you can do:

this.checkIf("email").LengthIs(xx); and so on..

Upvotes: 1

Related Questions