Skip to content

Instantly share code, notes, and snippets.

Last active Nov 12, 2017
What would you like to do?
const Cat = Mongoose.model('Cat', { name: String });
const CatProto = Object.getPrototypeOf(Cat); // this is the clean way to access an object's prototype. Please don't use __proto__ anymore.
const find = CatProto.find; // we take a reference on the orignial 'find method'
CatProto.find = function (...args) { // we replace the 'find' method on the prototype
const res = find.apply(this, args); // we call the original method we overrided
const exec = res.exec;
res.exec = async function () { // exec is the method actually firing the request on mongoose.
try {
const result = await exec.apply(this, arguments);
return result;
catch (err) {
throw err;
return res;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment