Skip to content

Commit

Permalink
fix(populate): handle doc.populate() with virtuals
Browse files Browse the repository at this point in the history
Fix #5240
  • Loading branch information
vkarpov15 committed May 21, 2017
1 parent a6dd311 commit dc6f887
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ Document.prototype.init = function(doc, opts, fn) {
}

this.isNew = false;
this.$init = true;

// handle docs with populated paths
// If doc._id is not null or undefined
Expand Down
10 changes: 9 additions & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var cast = require('./cast');
var castUpdate = require('./services/query/castUpdate');
var discriminator = require('./services/model/discriminator');
var isPathSelectedInclusive = require('./services/projection/isPathSelectedInclusive');
var mpath = require('mpath');
var parallel = require('async/parallel');
var util = require('util');
var utils = require('./utils');
Expand Down Expand Up @@ -3190,7 +3191,14 @@ function assignVals(o) {
if (o.isVirtual && !o.justOne && !Array.isArray(rawIds[i])) {
rawIds[i] = [rawIds[i]];
}
utils.setValue(o.path, rawIds[i], docs[i], setValue);

if (o.isVirtual && docs[i].constructor.name === 'model' && docs[i].$init) {
// If virtual populate and doc is already init-ed, need to walk through
// the actual doc to set rather than setting `_doc` directly
mpath.set(o.path, rawIds[i], docs[i]);
} else {
utils.setValue(o.path, rawIds[i], docs[i], setValue);
}
}
}

Expand Down

0 comments on commit dc6f887

Please sign in to comment.