Call instance method from instance method Call instance method from instance method mongoose mongoose

Call instance method from instance method


Do not use the arrow function. It uses the this context from the lexical scope of the function definition. And it's undefined in the strict mode you're using.

Use regular functions:

TestSchema.methods = {  method1: function() {      return true;  },  method2: function() {    if(this.method1()){        console.log('It works!');    }  }};

And then make sure to call the function as a method on the object:

TestSchema.methods.method2();

You can find more explanations about arrow functions as methods here.


This error occurs simply because of the arrow functions. The arrow function expression doesn't bind its own this, arguments, super and new.target.

Also, you shouldn't use function keyword to overcome this issue. The best solution is to use Shorthand method names.

TestSchema.methods = {  method1(){    return true;  },  method2(){    if (this.method1()) {      console.log('It works!');    }  }};TestSchema.methods.method2();