1

I am new to moogoose, i am running into the difficult to update object in the db.

here is my shcema.

var FormSchema = new Schema({
  formContent : {
    type : Object,
    required : true
  }, 
  formName : String,
  createdDateTime : Date
});

Here is the controller to update the field, i have used '[]' and it works to clean up the field, but whenever i PUT a new object and try to replace, and it just stay the same... any idea or suggestion would be much appreciated. stuck in this for hrs...

// Updates an existing form in the DB.
exports.update = function(req, res) {
  if(req.body._id) { delete req.body._id; }
  Form.findById(req.params.id, function (err, form) {
    if (err) { return handleError(res, err); }
    if(!form) { return res.send(404); }
    var updated = _.merge(form, req.body);
    // updated.formContent = [];
    updated.save(function (err) {
      if (err) { return handleError(res, err); }
      return res.json(200, form);
    });
  });
};
Bill
  • 17,872
  • 19
  • 83
  • 131

1 Answers1

1

Try using _.extend or _.assign instead:

var updated = _.assign(form, req.body);

This answer by ShitalShah highlights the differences between merge and extend:

Here's how extend/assign works: For each property in source, copy its value as-is to destination. if property values themselves are objects, there is no recursive traversal of their properties. Entire object would be taken from source and set in to destination.

Here's how merge works: For each property in source, check if that property is object itself. If it is then go down recursively and try to map child object properties from source to destination. So essentially we merge object hierarchy from source to destination. While for extend/assign, it's simple one level copy of properties from source to destination.

JSBin to illustrate the differences.

exports.update = function(req, res) {
  if(req.body._id) { delete req.body._id; }
  Form.findById(req.params.id, function (err, form) {
    if (err) { return handleError(res, err); }
    if(!form) { return res.send(404); }
    var updated = _.assign(form, req.body);
    // updated.formContent = [];
    updated.save(function (err) {
      if (err) { return handleError(res, err); }
      return res.json(200, form);
    });
  });
};
Community
  • 1
  • 1
chridam
  • 100,957
  • 23
  • 236
  • 235
  • 1
    Thank you again for the JSBin. – Bill Jun 19 '15 at 09:43
  • @bluebill1049 No worries mate ;-) – chridam Jun 19 '15 at 09:44
  • Sorry be a pain, Any reason why merge will end up stop updating the field? I really want get this uderstood – Bill Jun 19 '15 at 09:45
  • @bluebill1049 In addition to the provided explanation in the answer, this [**gist**](https://gist.github.com/creynders/8934667) explains the differences but basically [**`_.merge()`**](https://lodash.com/docs#merge) will recursively merge the object hierarchy from source to destination and thus it just stays the same. – chridam Jun 19 '15 at 10:07
  • @bluebill1049 No problem, glad to be of help. – chridam Jun 19 '15 at 13:25