-
-
Save aheckmann/2207966 to your computer and use it in GitHub Desktop.
Mongoose does not populate virtuals? :(
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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