Skip to content

Commit

Permalink
fix(query): ensure updateOne() doesnt unintentionally double call Sch…
Browse files Browse the repository at this point in the history
…ema#post(regexp)

Fix #7418
  • Loading branch information
vkarpov15 committed Jan 17, 2019
1 parent ded7dab commit f6db3ba
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions lib/schema.js
Expand Up @@ -23,9 +23,9 @@ const validateRef = require('./helpers/populate/validateRef');

let MongooseTypes;

const allMiddleware = require('./helpers/query/applyQueryMiddleware').
middlewareFunctions.
concat(require('./helpers/model/applyHooks').middlewareFunctions);
const queryHooks = require('./helpers/query/applyQueryMiddleware').
middlewareFunctions;
const documentHooks = require('./helpers/model/applyHooks').middlewareFunctions;

let id = 0;

Expand Down Expand Up @@ -1099,9 +1099,14 @@ Schema.prototype.queue = function(name, args) {
Schema.prototype.pre = function(name) {
if (name instanceof RegExp) {
const remainingArgs = Array.prototype.slice.call(arguments, 1);
for (const fn of allMiddleware) {
for (const fn of queryHooks) {
if (name.test(fn)) {
this.pre.apply(this, [fn].concat(remainingArgs));
this.pre.apply(this, [fn, { query: true, document: false }].concat(remainingArgs));
}
}
for (const fn of documentHooks) {
if (name.test(fn)) {
this.pre.apply(this, [fn, { query: false, document: true }].concat(remainingArgs));
}
}
return this;
Expand Down Expand Up @@ -1150,9 +1155,14 @@ Schema.prototype.pre = function(name) {
Schema.prototype.post = function(name) {
if (name instanceof RegExp) {
const remainingArgs = Array.prototype.slice.call(arguments, 1);
for (const fn of allMiddleware) {
for (const fn of queryHooks) {
if (name.test(fn)) {
this.post.apply(this, [fn, { query: true, document: false }].concat(remainingArgs));
}
}
for (const fn of documentHooks) {
if (name.test(fn)) {
this.post.apply(this, [fn].concat(remainingArgs));
this.post.apply(this, [fn, { query: false, document: true }].concat(remainingArgs));
}
}
return this;
Expand Down

0 comments on commit f6db3ba

Please sign in to comment.