node.js - More than 2 levels of population with mongoose -
i trying use mongoose populate ability return 1 composite document. categories , questions expanded fine. answers array in questions not expanded. here's have been doing
summaryschema.statics.loadfull = function(options, callback){ var self = this; self.findone(options).populate([{ path: 'categories' }]) .exec(function(err, res){ if(err || !res) return callback(err, res); self.populate(res, [{ path: 'categories.questions', model: 'question' }], function(err, res){ if(err || !res || !res.length) return callback(err, res); self.populate(res, [{ path : 'categories.questions.answers', model: 'answer' }], function(err, res){ callback(err, res); }); }); }); };
is there obvious mistake making or mongoose not support more 2 levels of population.
fyi, structure
summary {categories : [{type: mongoose.schema.types.objectid, ref: 'category'}]} category {questions : [{type: mongoose.schema.types.objectid, ref: 'question'}]} question {answers : [{type: mongoose.schema.types.objectid, ref: 'answer'}]}
i making silly mistake might not obvious unless see returned data. should not checking length on object that's not array or string. got return values confused. :)
Comments
Post a Comment