Created
March 26, 2012 11:50
-
-
Save thatmarvin/2204602 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'); | |
var Schema = mongoose.Schema; | |
var UserSchema = new Schema({ | |
name: { | |
first: { | |
type: String | |
}, | |
last: { | |
type: String | |
} | |
} | |
}); | |
User | |
.virtual('name.full') | |
.get(function () { | |
return [this.name.first, this.name.last].join(' '); | |
}); | |
var ItemSchema = new Schema({ | |
owner: { | |
type: Schema.ObjectId, | |
ref: 'User' | |
} | |
}); | |
var ItemModel = mongoose.model('User', UserSchema); | |
ItemModel | |
.findById(id) | |
.populate('owner') | |
.run(function (err, item) { | |
console.log(item.user.full); // == undefined | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment