8

Here is a code snippet of an update method using express.js and mongoose. I'm trying to merge the existing mongo entity with the json object from the request payload body.

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 doesn't work. I'm getting this error

node_modules/mongoose/lib/document.js:1272
    doc.save(handleSave);
        ^
TypeError: Object #<Object> has no method 'save'

I have try to create my own custom merge method but still i cannot achieve a proper 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)//_.merge(resume, req.body);
        updated.save(function (err) {
            if (err) { return handleError(res, err); }
            return res.json(200, entity);
        });
    });
};
function merger (a, b) {
    if (_.isObject(a)) {
        return _.merge({}, a, b, merger);
    } else {
        return a;
    }
};

With this variance i'm having this message :

node_modules/mongoose/lib/document.js:1245
      return self.getValue(i);
              ^
TypeError: Object #<Object> has no method 'getValue'

As a result, I am not able to extend the values of entity, and req.body to the destination updated. Only the structure gets copied i guess. Some one please let me know where I am wrong. Thanks.

Michael
  • 2,436
  • 1
  • 36
  • 57
  • 1
    It should work fine... I did the same thing with `underscore.extend` and it worked. Anyway, you could use `entity.set(req.body)` instead. – Leonid Beschastny Aug 22 '14 at 13:48
  • Thanks @LeonidBeschastny, this works for me but still i don't understand why the `_.merge`doesn't work, any idea? – Michael Aug 22 '14 at 14:27
  • Try using `_.merge` ignoring its return value: `merger(entity, req.body); entity.save(...)`. If it'll work then I have an idea of what's wrong. – Leonid Beschastny Aug 22 '14 at 14:31

1 Answers1

10

I was able to fix this by changing _.merge to _.extend, then calling save directly on the results returned by findById instead of the variable updated.

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); }
         _.extend(entity, req.body);
        entity.save(function (err) {
            if (err) { return handleError(res, err); }
            return res.json(200, entity);
        });
    });
};
Michael
  • 2,436
  • 1
  • 36
  • 57