Skip to content

Commit

Permalink
fix(populate): make ArraySubdocument#populated() return a value whe…
Browse files Browse the repository at this point in the history
…n the path is populated

Re: #8247
  • Loading branch information
vkarpov15 committed Oct 20, 2019
1 parent 8ddfc0a commit 25b6798
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions lib/document.js
Expand Up @@ -486,8 +486,7 @@ Document.prototype.$__init = function(doc, opts) {

// handle docs with populated paths
// If doc._id is not null or undefined
if (doc._id !== null && doc._id !== undefined &&
opts.populated && opts.populated.length) {
if (doc._id != null && opts.populated && opts.populated.length) {
const id = String(doc._id);
for (let i = 0; i < opts.populated.length; ++i) {
const item = opts.populated[i];
Expand All @@ -501,6 +500,8 @@ Document.prototype.$__init = function(doc, opts) {

init(this, doc, this._doc, opts);

markArraySubdocsPopulated(this, opts.populated);

this.emit('init', this);
this.constructor.emit('init', this);

Expand All @@ -509,6 +510,44 @@ Document.prototype.$__init = function(doc, opts) {
return this;
};

/*!
* If populating a path within a document array, make sure each
* subdoc within the array knows its subpaths are populated.
*
* ####Example:
* const doc = await Article.findOne().populate('comments.author');
* doc.comments[0].populated('author'); // Should be set
*/

function markArraySubdocsPopulated(doc, populated) {
if (doc._id == null || populated == null || populated.length === 0) {
return;
}

const id = String(doc._id);
for (const item of populated) {
if (item.isVirtual) {
continue;
}
const path = item.path;
const pieces = path.split('.');
for (let i = 0; i < pieces.length - 1; ++i) {
const subpath = pieces.slice(0, i + 1).join('.');
const rest = pieces.slice(i + 1).join('.');
const val = doc.get(subpath);
if (val == null) {
continue;
}
if (val.isMongooseDocumentArray) {
for (let j = 0; j < val.length; ++j) {
val[j].populated(rest, item._docs[id][j], item);
}
break;
}
}
}
}

/*!
* Init helper.
*
Expand Down

0 comments on commit 25b6798

Please sign in to comment.