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
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With