node.js - Mongoose - access populate field from within schema -


i have schema references model. like

var bookschema = new schema({     title: string,     series: { type: schema.types.objectid, ref: 'series' } }); 

now have function in book schema needs access series. want like

bookschema.methods.fulltitle = function() {     return [this.series.title, this.title].join(" - "); } 

but doesn't work.

how can this?

you need populate referenced model prior being able access properties. using existing setup, similar following:

bookschema.methods.fulltitle = function() {   this.populate('series', function(err, result) {     return [result.series.title, result.title].join(" - ");   }); } 

see mongoose population more details on this. http://mongoosejs.com/docs/populate.html


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -