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

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

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -