Javascript _.合并克隆子文档而不是更新
在我的Javascript _.合并克隆子文档而不是更新,javascript,angularjs,mongodb,merge,lodash,Javascript,Angularjs,Mongodb,Merge,Lodash,在我的Angular fullstack项目中,我尝试更新一个项目,向其中添加一个子文档。当在合并之前检查我的req.body时,所有内容看起来都应该如此。但是,在合并后,更新的文档已经添加了一个新的子文档,唯一的问题是,这个新文档是一个已经存在的文档的克隆,而不是它应该已经存在的文档 合并来自lodash 守则: console.log(''); console.log('notes before merging : ', entry.notes); console.
Angular fullstack
项目中,我尝试更新一个项目,向其中添加一个子文档。当在合并之前检查我的req.body
时,所有内容看起来都应该如此。但是,在合并后,更新的文档已经添加了一个新的子文档,唯一的问题是,这个新文档是一个已经存在的文档的克隆,而不是它应该已经存在的文档
合并
来自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: []
}
]
和我的控制台在运行代码时的结果:
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: []
}
]
合并前的注释:
[
{
内容:“测试”,
日期:2015年4月16日星期四10:14:44 GMT+0200,
作者:55193679026666b00554d00e,
_识别号:552F6F74358484716F6103,
注:[]
}
]
合并前的body.notes:
[
{
内容:“测试”,
日期:2015年4月16日星期四10:14:44 GMT+0200,
作者:55193679026666b00554d00e,
_识别号:552F6F74358484716F6103,
注:[]
},
{
内容:“bla-bla”,
日期:“2015-04-16T08:25:24.431Z”,
作者:55193679026666b00554d00e
}
]
合并后的注意事项:
[
{
内容:“测试”,
日期:2015年4月16日星期四10:14:44 GMT+0200,
作者:55193679026666b00554d00e,
_识别号:552F6F74358484716F6103,
注:[]
},
{
内容:“测试”,
日期:2015年4月16日星期四10:14:44 GMT+0200,
作者:55193679026666b00554d00e,
_识别号:552F6F74358484716F6103,
注:[]
}
]
尝试使用
扩展
或改为:
ShitalShah的这篇文章强调了合并和扩展之间的差异,这两种差异导致使用合并生成的对象中出现重复,但本质上:
以下是扩展/分配的工作原理:对于源中的每个属性,复制其
值按原样发送到目的地。如果特性值本身是对象,
它们的属性没有递归遍历。整个对象
将从源获取并设置到目标
以下是合并的工作原理:对于源中的每个属性,检查
属性是对象本身。如果是,则递归地向下并尝试
将子对象属性从源映射到目标。所以
本质上,我们将对象层次结构从源合并到目标。
而对于extend/assign,它只是从
从源到目标
为了说明差异:
var dest = {
p: { x: 10, y: 20},
};
var src = {
p: { x: 20, z: 30},
};
console.log(_.merge(dest, src));
/*
[object Object] {
p: [object Object] {
x: 20,
y: 20,
z: 30
}
}
*/
console.log(_.extend(dest, src));
/*
[object Object] {
p: [object Object] {
x: 20,
z: 30
}
}
*/