Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Forked from thatmarvin/gist:2204602
Created March 26, 2012 18:02
Show Gist options
  • Save aheckmann/2207966 to your computer and use it in GitHub Desktop.
Save aheckmann/2207966 to your computer and use it in GitHub Desktop.
Mongoose does not populate virtuals? :(
var mongoose = require('mongoose');
mongoose.connect('localhost', 'testing_populatedGetter');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
name: {
first: {
type: String
},
last: {
type: String
}
}
});
UserSchema.virtual('name.full').get(function() {
return [this.name.first, this.name.last].join(' ');
});
var ItemSchema = new Schema({
owner: {
type: Schema.ObjectId,
ref: 'User'
}
});
var Item = mongoose.model('Item', ItemSchema);
var User = mongoose.model('User', UserSchema);
mongoose.connection.on('open', function () {
new User({ name: { first: 'aaron', last: 'heckmann' }}).save(function (err, u) {
console.error('err1', err);
var item = new Item({ owner: u._id });
item.save(function (err) {
console.error('err2', err);
Item.findOne().populate('owner').run(function(err, item) {
if (err) console.error('err3', err);
console.log('virtual: %s',item.owner.name.full);
mongoose.disconnect();
});
});
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment