javascript - Updating subarray object with Mongoose not working -


my mongodb database main document looks this:

    {         "_id": {             "$oid": "568ad3db59b494d4284ac191"         },         "name": "myclass",         "items": [           {             "name": "conductive gel",             "internal_id": "id00067",             "description": "ecg conductive gel",             "short_description": "ecg conduct. gel",             "providers": [               {                 "name": "one",                 "address": ""               },               {                 "name": "two",                 "address": ""               }             ]            },           {            }         ]    } 

ok thing receiving ajax put call should update 1 of items (the 1 matches _id).

my approach:

exports.updateitem = function(req, res, next) {                  classes.findone({_id: '568ad3db59b494d4284ac19d'}, function(e,myclass){                     if(!e) {                        myclass.items.foreach(function(item){                         if (item._id == req.body._id) {                              item = req.body;                             myclass.save(function(err, doc){                                   if (err) return next(err);                                 return res.status(200).send('the item has been updated!');                             });                         }                                                     });                      } else {                         res.status(500).send('class not folund in bbdd!!');                     }                 }); }; 

the thing when item = req.body; req.body info not mapped item mongoose object , item in database not updated. don't error either.

i have checked req.body , item both have exact same fields in moment item = req.body; .

if item.name='whatever' , on other hand, works.

i've been fighting issue 4 hours without solution...

i have tried mongoose's findoneandupdate() query without success..

if assign item new value not changing content of array. item reference element in array.

what want edit content of array merging 2 objects item , req.body.

require('extend'); // npm install extend extend(item, req.body); 

that update value in array saved.

however, recommend updating subdocument using mongoose explained here: mongoose find/update subdocument


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -