Lodash merge with mongoose

蹲街弑〆低调 提交于 2019-12-05 05:49:14

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);
        });
    });
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!