How to override mongoose plugin function?

I have a plugin in mongoose and for each circuit I do the following

var user = new Schema({});
user.plugin(myplugin);
user.statics.createCustomDoc = function() {
.....
}

The problem is that the createCustomDoc method is also defined in myplugin. Now I want to override createCustomDocmyplugin using the method defined as user.statics.createCustomDoc.

Currently, the method called from the plugin is not the one I wrote in user.statics.createCustomDoc.

How to do it?

Of course, I do not want to change the name of the function, and I do not want to remove the plugin, and I do not want to change the code of the plugin.

+4
source share
1 answer

, - ,

//store reference to original myPlugin createCustomDoc function
var createCustomDoc = user.statics.createCustomDoc;

//override that function
user.statics.createCustomDoc = function (options, callback) {
   var self = this; 
   //after your code, call original function
   createCustomDoc.call(self, options, callback);

}
0

Source: https://habr.com/ru/post/1534040/


All Articles