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.
-
1
It should work fine... I did the same thing with
underscore.extend
and it worked. Anyway, you could useentity.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
_.merge
doesn'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
1 Answer
Reset to default 11I 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
版权声明:本文标题:javascript - Lodash merge with mongoose - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741500769a2382044.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论