I have following schema.
var ItemSchema = new Schema({
name : String
,location: {
address: { type: String, default:''},
geolocation: {longitude: Number, latitude:Number},
place : {type: Schema.Types.ObjectId, ref:'Place'}
},
ranking_in_place : Number })
Place is a reference to Place schema which has name, city, country etc. fields.
I want to create a virtual for ranking_summary:
ItemSchema.virtual('ranking_summary').get(function() {
if(this.ranking_in_place <= 5){
if(this.ranking_in_place == 1){
return "Most popular item" + " in " + this.location.place.name
}
}
})
I cannot get this.location.place.name value because this.location.place is a reference, and not populated. How can I access this value?
Have you made sure to call .populate() on your queries? Otherwise, Mongoose won’t know to pull in the reference object. Example: