admin管理员组

文章数量:1290926

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.

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.

Share Improve this question asked Aug 22, 2014 at 13:35 MichaelMichael 2,5661 gold badge41 silver badges59 bronze badges 3
  • 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 Commented Aug 22, 2014 at 13:48
  • Thanks @LeonidBeschastny, this works for me but still i don't understand why the _.mergedoesn't work, any idea? – Michael Commented Aug 22, 2014 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 Commented Aug 22, 2014 at 14:31
Add a ment  | 

1 Answer 1

Reset to default 11

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);
        });
    });
};

本文标签: javascriptLodash merge with mongooseStack Overflow