Hi - I have (simplified) schemas as follows:
var PersonSchema = new Schema({
firstName: String,
familyName: String,
});
mongoose.model('Person', PersonSchema);
var Person = mongoose.model('Person');
var ContactSchema = new Schema({
contact : { type: Schema.ObjectId, ref: 'Person' },
type: { type: String},
relationship: String,
start: Date,
finish: Date
});
var ClientSchema = new Schema({
_self : { type: Schema.ObjectId, ref: 'Person' },
contactList: [ContactSchema],
});
I want to read the client name every time the record is used (but keep it in the person collection for reasons that are too complicated to go into here). I was expecting the following to work:
ClientSchema.post("init", function(next, doc) {
if (!this._self)
return next();
this.populate('_self');
return next();
});
but I am getting
TypeError: Object { _self: 5012866355c1bf942b000011, _id: 5012866355c1bf942b000010, contactList:
[ { type: 'supplier',
contact: 5012866355c1bf942b000014,
_id: 5012866355c1bf942b000017 } ] } has no method 'populate'
What am I doing wrong?
Mark