Here is the code snippet of the update method using express.js and mongoose. I am trying to combine an existing mongo object with a json object from the body of the request payload.
exports.update = function(req, res) { if(req.body._id) { delete req.body._id; } Entity.findById(req.params.id, function (err, entity) { if (err) { return handleError(res, err); } if(!entity) { return res.send(404); } var updated = _.merge(entity, req.body); updated.save(function (err) { if (err) { return handleError(res, err); } return res.json(200, entity); }); }); };
This, unfortunately, does not work. I get this error
node_modules/mongoose/lib/document.js:1272 doc.save(handleSave); ^ TypeError: Object
I am trying to create my own merge method, but still I cannot achieve the correct merge:
exports.update = function(req, res) { if(req.body._id) { delete req.body._id; } Entity.findById(req.params.id, function (err, entity) { if (err) { return handleError(res, err); } if(!entity) { return res.send(404); } var updated = merger(resume, req.body)
With this dispersion, I have this message:
node_modules/mongoose/lib/document.js:1245 return self.getValue(i); ^ TypeError: Object #<Object> has no method 'getValue'
As a result, I cannot extend the entity and req.body to the updated destination. I think only the structure is copied. Someone please let me know where I am going wrong. Thanks.
Michael
source share