Skip to content

Commit dc6f887

Browse files
committed
fix(populate): handle doc.populate() with virtuals
Fix #5240
1 parent a6dd311 commit dc6f887

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/document.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ Document.prototype.init = function(doc, opts, fn) {
311311
}
312312

313313
this.isNew = false;
314+
this.$init = true;
314315

315316
// handle docs with populated paths
316317
// If doc._id is not null or undefined

lib/model.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var cast = require('./cast');
1919
var castUpdate = require('./services/query/castUpdate');
2020
var discriminator = require('./services/model/discriminator');
2121
var isPathSelectedInclusive = require('./services/projection/isPathSelectedInclusive');
22+
var mpath = require('mpath');
2223
var parallel = require('async/parallel');
2324
var util = require('util');
2425
var utils = require('./utils');
@@ -3190,7 +3191,14 @@ function assignVals(o) {
31903191
if (o.isVirtual && !o.justOne && !Array.isArray(rawIds[i])) {
31913192
rawIds[i] = [rawIds[i]];
31923193
}
3193-
utils.setValue(o.path, rawIds[i], docs[i], setValue);
3194+
3195+
if (o.isVirtual && docs[i].constructor.name === 'model' && docs[i].$init) {
3196+
// If virtual populate and doc is already init-ed, need to walk through
3197+
// the actual doc to set rather than setting `_doc` directly
3198+
mpath.set(o.path, rawIds[i], docs[i]);
3199+
} else {
3200+
utils.setValue(o.path, rawIds[i], docs[i], setValue);
3201+
}
31943202
}
31953203
}
31963204

0 commit comments

Comments
 (0)