Skip to content

Commit a6dd311

Browse files
committed
test(populate): repro #5240
1 parent 6a2d405 commit a6dd311

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

test/model.populate.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5053,6 +5053,40 @@ describe('model: populate:', function() {
50535053
catch(done);
50545054
});
50555055

5056+
it('nested virtuals + doc.populate() (gh-5240)', function(done) {
5057+
var parentSchema = new Schema({ name: String });
5058+
var childSchema = new Schema({
5059+
parentId: mongoose.Schema.Types.ObjectId
5060+
});
5061+
childSchema.virtual('parent', {
5062+
ref: 'gh5240',
5063+
localField: 'parentId',
5064+
foreignField: '_id',
5065+
justOne: true
5066+
});
5067+
var teamSchema = new Schema({ people: [childSchema] });
5068+
5069+
var Parent = db.model('gh5240', parentSchema);
5070+
var Team = db.model('gh5240_0', teamSchema);
5071+
5072+
Parent.create({ name: 'Darth Vader' }).
5073+
then(function(doc) {
5074+
return Team.create({ people: [{ parentId: doc._id }] });
5075+
}).
5076+
then(function(team) {
5077+
return Team.findById(team._id);
5078+
}).
5079+
then(function(team) {
5080+
return team.populate('people.parent').execPopulate();
5081+
}).
5082+
then(function(team) {
5083+
team = team.toObject({ virtuals: true });
5084+
assert.equal(team.people[0].parent.name, 'Darth Vader');
5085+
done();
5086+
}).
5087+
catch(done);
5088+
});
5089+
50565090
it('virtual populate in single nested doc (gh-4715)', function(done) {
50575091
var someModelSchema = new mongoose.Schema({
50585092
name: String

0 commit comments

Comments
 (0)