node.js - _id not being generated for subdocs when It's supposed to -


here 2 schemas

var reviews = new schema({     scenarioid: { type: schema.types.objectid, required: true},     authorid:  { type: schema.types.objectid , required:true },     reviewnote:   string,     subreviews: [subreviewschema],     active: {type: boolean, default: true},     display:  {type: boolean, default: true},     date : {type: date, default: date.now()} }); 

and subschea subreviews

var subreviews = new schema({     authorid:  { type: schema.types.objectid, required:true },     subreviewnote: string,     date: {type: date, default: date.now()},     active: {type: boolean, default: true},     display:  {type: boolean, default: true} }); 

and here code updates document

exports.addsubreview = function (req, res) {     var id = req.params.id;     var update = req.body;//scenario id      review.findbyid(id, function (err, obj) {         if (err || !obj) { return res.send(404, { error: err.message }); }          obj.subreviews.push(update);          obj.save(function (err) {             if (err) { return res.send(404, { error: err.message }); }              return res.send(200, obj);         });     }); }; 

for reason though whenever send http post code results adds send in post request not _id _v or other things expect mongoose/mongodb add boilerplate. here example document in database

{     "__v": 2,     "_id": "531e3214a30f5f8427830a97",     "authorid": "52fd0e6df8352c184b000004",     "reviewnote": "aaaaaaaaaaaaaaaaa",     "scenarioid": "531a5b80af15cffc051cea67",     "date": "2014-03-10t21:37:05.230z",     "display": true,     "active": true,     "subreviews": [         {             "subreviewnote": "this subreview",             "authorid": "52fd0e6df8352c184b000004"         },         {             "subreviewnote": "this subreview",             "authorid": "52fd0e6df8352c184b000004"         }     ] }  

any ideas on why _id not being added subdocs in subreviews?.

my guess problem in parent doc say:

subreviews: [subreviewschema]

but named child schema variable

subreviews

not subreviewschema. that's guess, you've posted. i'd have see code better picture, unless it.

but explain it, since subreviews: expecting object because of naming issue - , object gets when post,so pushes array expects.

edit

i poked around in mongoose code on github , less confident in answer above, although guess still possible. however, did stumble upon comment, when declaring schemas:

  • when nesting schemas, (children in example above), declare child schema first before passing parent.

i have less confidence in original answer, because looks if named variable incorrectly, mongoose going throw typeerror


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -