mongodb - Populate nested array in mongoose - Node.js -
these schemas (topic parent , contains list of 'thought's):
var topicschema = new mongoose.schema({ title: { type: string, unique: true }, category: string, thoughts: [thoughtschema] }, { timestamps: true, toobject: {virtuals: true}, tojson: {virtuals: true} }); var thoughtschema = new mongoose.schema({ text: string, author: {type: mongoose.schema.types.objectid, ref: 'user'}, votes:[{ _id:false, voter: {type: mongoose.schema.types.objectid, ref: 'user'}, up: boolean, date: {type: date, default: date.now} }] }, { timestamps: true, toobject: {virtuals: true}, tojson: {virtuals: true} }); ....
i trying read thought's author , change topic api this:
... var cursor = topic.find(query).populate({ path: 'thoughts', populate: { path: 'author', model: 'user' } }).sort({popularity : -1, date: -1}); return cursor.exec() .then(respondwithresult(res)) .catch(handleerror(res)); ...
but author null.. not error in console. wrong here?
edit: not need thought schema, not have own collection in database. saved in topics. in order use timestamps option thoughts, needed extract contents new local schema thoughtschema. have defined contents of thoughtschema directly in thoughts array of topics, still not work.
edit2: cursor object before executed. unfortunately cannot debug in webstorm, screenshot node inspector:
how about
topic.find(query).populate('thoughts') .sort({popularity : -1, date: -1}) .exec(function(err, docs) { // multiple population per level if(err) return callback(err); topic.populate(docs, { path: 'thoughts.author', model: 'user' }, function(err, populateddocs) { if(err) return callback(err); console.log(populateddocs); }); });
Comments
Post a Comment