Skip to content

Commit

Permalink
refactor(populate): move execPopulateQuery() calls after all querie…
Browse files Browse the repository at this point in the history
…s are computed re: #3812
  • Loading branch information
vkarpov15 committed Sep 6, 2019
1 parent af14c61 commit 39566f1
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4124,6 +4124,7 @@ function populate(model, docs, options, callback) {

let _remaining = len;
let hasOne = false;
const params = [];
for (let i = 0; i < len; ++i) {
const mod = modelsMap[i];
let select = mod.options.select;
Expand Down Expand Up @@ -4189,11 +4190,7 @@ function populate(model, docs, options, callback) {
mod.options.options.limit = mod.options.options.limit * ids.length;
}

_execQuery(mod, match, select, (err, valsFromDb) => {
if (mod.options.options && mod.options.options.limit) {
mod.options.options.limit = assignmentOpts.originalLimit;
}

params.push([mod, match, select, assignmentOpts, (err, valsFromDb) => {
if (err != null) {
return callback(err, null);
}
Expand All @@ -4202,15 +4199,23 @@ function populate(model, docs, options, callback) {
if (--_remaining === 0) {
callback();
}
});
}]);
}

if (!hasOne) {
return callback();
}

for (const arr of params) {
_execPopulateQuery.apply(null, arr);
}
}

function _execQuery(mod, match, select, callback) {
/*!
* ignore
*/

function _execPopulateQuery(mod, match, select, assignmentOpts, callback) {
const subPopulate = utils.clone(mod.options.populate);

const query = mod.model.find(match, select, mod.options.options);
Expand Down Expand Up @@ -4239,6 +4244,10 @@ function _execQuery(mod, match, select, callback) {
}

query.exec(callback);

if (mod.options.options && mod.options.options.limit) {
mod.options.options.limit = assignmentOpts.originalLimit;
}
}

/*!
Expand Down

0 comments on commit 39566f1

Please sign in to comment.