Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/helpers/populate/getModelsMapForPopulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
modelNames = [modelNameFromQuery]; // query options
} else if (refPath != null) {
if (typeof refPath === 'function') {
const subdocPath = options.path.slice(0, options.path.length - schema.path.length - 1);
const subdocPath = options.path.slice(0, Math.max(0, options.path.length - schema.path.length - 1));
const vals = mpath.get(subdocPath, doc, lookupLocalFields);
const subdocsBeingPopulated = Array.isArray(vals) ?
utils.array.flatten(vals) :
Expand Down Expand Up @@ -301,7 +301,7 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
// Ensure correct context for ref functions: subdoc, not top-level doc. See gh-8469
modelNames = new Set();

const subdocPath = options.path.slice(0, options.path.length - schemaForCurrentDoc.path.length - 1);
const subdocPath = options.path.slice(0, Math.max(0, options.path.length - schemaForCurrentDoc.path.length - 1));
const vals = mpath.get(subdocPath, doc, lookupLocalFields);
const subdocsBeingPopulated = Array.isArray(vals) ?
utils.array.flatten(vals) :
Expand All @@ -322,7 +322,7 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
} else if ((schemaForCurrentDoc = get(schema, 'options.refPath')) != null) {
isRefPath = true;
if (typeof refPath === 'function') {
const subdocPath = options.path.slice(0, options.path.length - schemaForCurrentDoc.path.length - 1);
const subdocPath = options.path.slice(0, Math.max(0, options.path.length - schemaForCurrentDoc.path.length - 1));
const vals = mpath.get(subdocPath, doc, lookupLocalFields);
const subdocsBeingPopulated = Array.isArray(vals) ?
utils.array.flatten(vals) :
Expand Down