From 037fbe55806aa4cbf353211bb5175237c3eb9807 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sun, 8 Sep 2019 16:02:33 -0600 Subject: [PATCH] refactor(populate): assign using all separate models after all queries are done re: #3812 --- lib/model.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/model.js b/lib/model.js index d9a5b176a95..a9a0784c50e 100644 --- a/lib/model.js +++ b/lib/model.js @@ -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) { @@ -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(); + } } /*!