Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a method from inside of another class

I have a node class like this:

var Classes = function () {
};

Classes.prototype.methodOne = function () {
    //do something
};

when i want to call methodOne, i use this:

this. methodOne();

And it works. But right now i have to call it from inside of another method of another class. This time its not works and cant access to methodOne :

var mongoose = new Mongoose();
mongoose.save(function (err, coll) {
  //save to database
  this. methodOne(); //this does not work
}

how i can call methodOne? i use Classes.methodOne() but it's not work

like image 783
Fcoder Avatar asked Nov 27 '25 20:11

Fcoder


1 Answers

this inside the save callback is in a new context and is a different this than the outside one. Preserve it in a variable where it has access to methodOne

var that = this;
mongoose.save(function (err, coll) {
  //save to database
  that.methodOne();
}
like image 50
AmmarCSE Avatar answered Nov 29 '25 08:11

AmmarCSE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!