Skip to content

Commit

Permalink
fix(query): avoid double-calling query transform() with findOne()
Browse files Browse the repository at this point in the history
Fix #14236
  • Loading branch information
vkarpov15 committed Jan 6, 2024
1 parent ad5f8a1 commit 3b2a138
Showing 1 changed file with 8 additions and 30 deletions.
38 changes: 8 additions & 30 deletions lib/query.js
Expand Up @@ -2498,12 +2498,12 @@ Query.prototype._findOne = async function _findOne() {
// don't pass in the conditions because we already merged them in
const doc = await this.mongooseCollection.findOne(this._conditions, options);
return new Promise((resolve, reject) => {
this._completeOne(doc, null, _wrapThunkCallback(this, (err, res) => {
this._completeOne(doc, null, (err, res) => {
if (err) {
return reject(err);
}
resolve(res);
}));
});
});
};

Expand Down Expand Up @@ -3303,12 +3303,12 @@ Query.prototype._findOneAndUpdate = async function _findOneAndUpdate() {
const doc = !options.includeResultMetadata ? res : res.value;

return new Promise((resolve, reject) => {
this._completeOne(doc, res, _wrapThunkCallback(this, (err, res) => {
this._completeOne(doc, res, (err, res) => {
if (err) {
return reject(err);
}
resolve(res);
}));
});
});
};

Expand Down Expand Up @@ -3399,12 +3399,12 @@ Query.prototype._findOneAndDelete = async function _findOneAndDelete() {
const doc = !includeResultMetadata ? res : res.value;

return new Promise((resolve, reject) => {
this._completeOne(doc, res, _wrapThunkCallback(this, (err, res) => {
this._completeOne(doc, res, (err, res) => {
if (err) {
return reject(err);
}
resolve(res);
}));
});
});
};

Expand Down Expand Up @@ -3553,12 +3553,12 @@ Query.prototype._findOneAndReplace = async function _findOneAndReplace() {

const doc = !includeResultMetadata ? res : res.value;
return new Promise((resolve, reject) => {
this._completeOne(doc, res, _wrapThunkCallback(this, (err, res) => {
this._completeOne(doc, res, (err, res) => {
if (err) {
return reject(err);
}
resolve(res);
}));
});
});
};

Expand Down Expand Up @@ -4382,28 +4382,6 @@ function _executePreHooks(query, op) {
});
}

/*!
* ignore
*/

function _wrapThunkCallback(query, cb) {
return function(error, res) {
if (error != null) {
return cb(error);
}

for (const fn of query._transforms) {
try {
res = fn(res);
} catch (error) {
return cb(error);
}
}

return cb(null, res);
};
}

/**
* Executes the query returning a `Promise` which will be
* resolved with either the doc(s) or rejected with the error.
Expand Down

0 comments on commit 3b2a138

Please sign in to comment.