Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lodash merge with mongoose

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.

like image 638
Michael Avatar asked Aug 22 '14 13:08

Michael


1 Answers

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);
        });
    });
};
like image 100
Michael Avatar answered Nov 16 '22 16:11

Michael