Using populate() in .post('init') - newbie question

483 views
Skip to first unread message

MarkUK

unread,
Jul 27, 2012, 8:24:05 AM7/27/12
to mongoo...@googlegroups.com
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

Aaron Heckmann

unread,
Jul 27, 2012, 5:44:52 PM7/27/12
to mongoo...@googlegroups.com
currently post hooks do not have next(), their behavior is identical to listening to doc.on('save', callback);

populate() is a method of Query, not of a document, though thats been lightly discussed. there's an open ticket related to that. 

here's a working mongoose v3 gist that does hack auto-prepopulation: https://gist.github.com/3190609


--
--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en
 
 
 



--
Aaron



Reply all
Reply to author
Forward
0 new messages