Javascript 猫鼬&x2B;lodash错误地扩展复制对象数组
我有这个模式Javascript 猫鼬&x2B;lodash错误地扩展复制对象数组,javascript,node.js,mongodb,mongoose,lodash,Javascript,Node.js,Mongodb,Mongoose,Lodash,我有这个模式 var CandidateProfileSchema = new Schema({ OtherExp: [{ typeExp:String, organization: String, startDate: String, endDate: String, role: String, description: String, achievements: String }], //more fields }); 这
var CandidateProfileSchema = new Schema({
OtherExp: [{
typeExp:String,
organization: String,
startDate: String,
endDate: String,
role: String,
description: String,
achievements: String
}],
//more fields
});
这是我的控制器函数,用于在模式中放置/更新OtherExp字段
exports.updateOtherExp = function(req, res) {
if(req.body._id) { delete req.body._id; }
CandidateProfile.findOne({userId:req.params.id}, function (err, candidateProfile) {
if (err) { return handleError(res, err); }
if(!candidateProfile) { return res.send(404); }
candidateProfile.other= _.extend(candidateProfile.other, req.body.other);
candidateProfile.save(function (err) {
if (err) { return handleError(res, err); }
return res.json(200, candidateProfile);
});
});
};
我的数据是说
第1行:a1、a2、a3、a4、a5、a6、a7
第2行:b1、b2、b3、b4、b5、b6、b7
问题是保存到我的mongodb集合的数据是第一行的重复
第1行:a1、a2、a3、a4、a5、a6、a7
第2行:a1、a2、a3、a4、a5、a6、a7
有人能看出问题所在吗?
同样的代码也适用于我的模式的其他部分,在这些部分中,我没有像下面这样的数据嵌套
这是来自我的候选文件/index.js
router.put('/:id', controller.update);
router.put('/:id/skills', controller.updateSkills);
router.put('/:id/otherExp', controller.updateOtherExp);
我认为这可能是一个打字错误: 如果您想更新您的candidateProfile中的OtherExp,它应该是这样的
candidateProfile.OtherExp = _.extend(candidateProfile.OtherExp, req.body.OtherExp);`
candidateProfile.save(//... etc)
带有嵌套可枚举项的Mongoose模型与lodash合并或扩展相混淆 在分配之前,请先尝试聚合数据
var data = _.merge(candidateProfile.toJSON(), req.body);
candidateProfile.other = _.extend(candidateProfile.other, data.other);
我在类似的问题上浪费了1个小时。我使用了
.assign{In}()
,然后使用了.merge()
,然后还尝试了文档#集()
我总是以数组中的重复条目结束
对我有效的变通方法
- 将
分配给即将设置的任何数组[]
- 然后使用
doc.set(attrs)
一些有问题的数组
导致了与上述相同的奇怪行为):
我将使用节点检查器来确保
candidateProfile.other
和req.body.other
是您期望的值。名称是otherExp,但这不是重点。我们可以将数据推送到mongodb数据库中,但这是两次第一次体验信息,而不是两次不同体验的数据。我已经测试了请求主体,以确认我们正在推送正确的数据。您是否可以尝试u.merge而不是extendal?发送的数据是什么样的,是要添加到OtherExp数组中的元素,还是要替换OtherExp的数组?因为如果你只想添加到数组中,你所要做的就是推送新元素candidateProfile.OtherExp.push(req.body.experience)
这就是我在数据库中得到的{typeExp:'Research',organization:'bleh',startDate:'01/01/2015',endDate:'05/01/2015',角色:'bleh',描述:'bleh',成就:'bleh',_id:54a9fcf541aa72381548c9d6},{typeExp:'Research',organization:'bleh',startDate:'01/01/2015',endDate:'05/01/2015',role:'bleh',description:'bleh',productions:'bleh',id:54a9fcf541aa72381548c9d6}我所期待的是{typeExp:'Research',organization:'bleh',startDate:'01/01/2015',endDate:'05/01/2015',角色:'bleh',描述:'bleh',成就:'bleh',_id:54a9fcf541aa72381548c9d6},{typeExp:'Intern',organization:'meh',startDate:'06/01/2015',endDate:'10/01/2015',role:'meh',description:'meh',congregations:'meh',_id:54a9fcf541aa72381548c9d6}你所说的“一团糟”是什么意思?
var attrs = _.pick(req.body, [
'name',
'tags', // ...
"some_problematic_array"
]);
var doc = ///... ;
if( attrs.some_problematic_array ) doc.some_problematic_array = [];
^^^^ ***workaround***
doc.set(attrs);