Skip to content

Commit

Permalink
fix: fix some merge issues with #6029 and #9340
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 7, 2021
1 parent 2489924 commit f797ddc
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions lib/helpers/populate/getModelsMapForPopulate.js
Expand Up @@ -71,6 +71,7 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
modelNames = null;
let isRefPath = false;
let normalizedRefPath = null;
let schemaOptions = null;

if (Array.isArray(schema)) {
const schemasArray = schema;
Expand Down Expand Up @@ -108,6 +109,7 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
isRefPath = res.isRefPath;
normalizedRefPath = res.refPath;
justOne = res.justOne;
schemaOptions = get(schema, 'options.populate', null);
} catch (error) {
return error;
}
Expand Down Expand Up @@ -164,7 +166,7 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
modelNames = embeddedDiscriminatorModelNames || modelNames;

try {
addModelNamesToMap(model, map, available, modelNames, options, data, ret, doc);
addModelNamesToMap(model, map, available, modelNames, options, data, ret, doc, schemaOptions);
} catch (err) {
return err;
}
Expand Down Expand Up @@ -360,7 +362,7 @@ function _virtualPopulate(model, docs, options, _virtualRes) {
* ignore
*/

function addModelNamesToMap(model, map, available, modelNames, options, data, ret, doc) {
function addModelNamesToMap(model, map, available, modelNames, options, data, ret, doc, schemaOptions) {
// `PopulateOptions#connection`: if the model is passed as a string, the
// connection matters because different connections have different models.
const connection = options.connection != null ? options.connection : model.db;
Expand All @@ -376,9 +378,21 @@ function addModelNamesToMap(model, map, available, modelNames, options, data, re
continue;
}

const Model = options.model && options.model[modelSymbol] ?
options.model :
modelName[modelSymbol] ? modelName : connection.model(modelName);
let Model;
if (options.model && options.model[modelSymbol]) {
Model = options.model;
} else if (modelName[modelSymbol]) {
Model = modelName;
} else {
try {
Model = connection.model(modelName);
} catch (err) {
if (ret !== void 0) {
return err;
}
Model = null;
}
}

let ids = ret;
const flat = Array.isArray(ret) ? utils.array.flatten(ret) : [];
Expand All @@ -398,6 +412,8 @@ function addModelNamesToMap(model, map, available, modelNames, options, data, re

if (data.isVirtual && get(data.virtual, 'options.options')) {
currentOptions.options = utils.clone(data.virtual.options.options);
} else if (schemaOptions != null) {
currentOptions.options = Object.assign({}, schemaOptions);
}
utils.merge(currentOptions, options);

Expand Down

0 comments on commit f797ddc

Please sign in to comment.