Skip to content

Commit

Permalink
refactor(populate): assign using all separate models after all querie…
Browse files Browse the repository at this point in the history
…s are done re: #3812
  • Loading branch information
vkarpov15 committed Sep 8, 2019
1 parent eb75b54 commit 037fbe5
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4190,16 +4190,7 @@ function populate(model, docs, options, callback) {
mod.options.options.limit = mod.options.options.limit * ids.length;
}

params.push([mod, match, select, assignmentOpts, (err, valsFromDb) => {
if (err != null) {
return callback(err, null);
}
vals = vals.concat(valsFromDb);
if (--_remaining === 0) {
_assign(model, vals, mod, assignmentOpts);
callback();
}
}]);
params.push([mod, match, select, assignmentOpts, _next]);
}

if (!hasOne) {
Expand All @@ -4209,6 +4200,25 @@ function populate(model, docs, options, callback) {
for (const arr of params) {
_execPopulateQuery.apply(null, arr);
}

function _next(err, valsFromDb) {
if (err != null) {
return callback(err, null);
}
vals = vals.concat(valsFromDb);
if (--_remaining === 0) {
_done();
}
}

function _done() {
for (const arr of params) {
const mod = arr[0];
const assignmentOpts = arr[3];
_assign(model, vals, mod, assignmentOpts);
}
callback();
}
}

/*!
Expand Down

0 comments on commit 037fbe5

Please sign in to comment.