【发布时间】:2015-06-22 13:16:46
【问题描述】:
在我的Angular fullstack 项目中,我尝试更新一个项目,添加一个新的子文档。在合并之前检查我的req.body 时,一切看起来都应该如此。然而,在合并之后,更新后的文档按原样添加了一个新的子文档,唯一的问题是这个新文档是一个已经存在的文档的克隆,而不是它应该是的那个。
_.merge 来自lodash
代码:
console.log('');
console.log('notes before merging : ', entry.notes);
console.log('');
console.log('body.notes before merging : ', req.body.notes);
var updated = _.merge(entry, req.body);
console.log('');
console.log('notes after merging : ', updated.notes);
运行代码时我的控制台结果:
notes before merging :
[
{
content: '<p>testing</p>',
date: Thu Apr 16 2015 10:14:44 GMT+0200 (Rom, sommertid),
writer: 55193679026666b00554d00e,
_id: 552f6f7435828478156f6103,
notes: []
}
]
body.notes before merging :
[
{
content: '<p>testing</p>',
date: Thu Apr 16 2015 10:14:44 GMT+0200 (Rom, sommertid),
writer: 55193679026666b00554d00e,
_id: 552f6f7435828478156f6103,
notes: []
},
{
content: '<p>bla bla</p>',
date: '2015-04-16T08:25:24.431Z',
writer: 55193679026666b00554d00e
}
]
notes after merging :
[
{
content: '<p>testing</p>',
date: Thu Apr 16 2015 10:14:44 GMT+0200 (Rom, sommertid),
writer: 55193679026666b00554d00e,
_id: 552f6f7435828478156f6103,
notes: []
},
{
content: '<p>testing</p>',
date: Thu Apr 16 2015 10:14:44 GMT+0200 (Rom, sommertid),
writer: 55193679026666b00554d00e,
_id: 552f6f7435828478156f6103,
notes: []
}
]
【问题讨论】:
标签: javascript angularjs mongodb merge lodash