【问题标题】:Mongoose + lodash extend copying array of object incorrectlyMongoose + lodash 错误地扩展了对象的复制数组
【发布时间】:2015-03-02 12:35:36
【问题描述】:

我有这个架构

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

谁能看到可能是什么问题? 相同的代码适用于我的架构的其他部分,我没有像这个一样嵌套数据。

这是来自我的 CandidateProfile / index.js

router.put('/:id', controller.update);
router.put('/:id/skills', controller.updateSkills);
router.put('/:id/otherExp', controller.updateOtherExp);

【问题讨论】:

  • 我会使用节点检查器来确保 candidateProfile.otherreq.body.other 是您期望的值。

标签: javascript node.js mongodb mongoose lodash


【解决方案1】:

我认为这可能是一个错字: 如果你想更新你的 CandidateProfile 中的 OtherExp,它应该是这样的

candidateProfile.OtherExp = _.extend(candidateProfile.OtherExp, req.body.OtherExp);`
candidateProfile.save(//... etc)

【讨论】:

  • 名称是 otherExp,但这不是重点。我们能够将数据推送到 mongodb 数据库中,但它是两次第一次体验信息,而不是两次不同体验的数据。我已经测试了请求正文,以确认我们正在推送正确的数据。
  • 你可以试试 _.merge 而不是 extend
  • 还有发送的数据是什么样的,是要添加到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:'研究',组织:'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 ',描述:'bleh',成就:'bleh',_id:54a9fcf541aa72381548c9d6 },{ typeExp:'实习生',组织:'meh',startDate:'06/01/2015',endDate:'10/01/2015 ',角色:'meh',描述:'meh',成就:'meh',_id:54a9fcf541aa72381548c9d6 }
【解决方案2】:

具有嵌套枚举的 Mongoose 模型与 lodash 合并或扩展混淆。

在分配之前先尝试聚合您的数据。

var data = _.merge(candidateProfile.toJSON(), req.body);
candidateProfile.other = _.extend(candidateProfile.other, data.other);

【讨论】:

  • “搞砸”是什么意思?
【解决方案3】:

我刚刚在类似问题上浪费了 1 个小时。我使用了_.assign{In}(),然后使用了_.merge(),然后还尝试了Document#set(),我总是以数组中的重复条目结束。

适合我的解决方法

  • [] 分配给任何即将设置的数组
  • 然后使用doc.set(attrs) 分配整棵树

示例(在我的情况下,some_problematic_array 引起了与问题相同的奇怪行为):

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    相关资源
    最近更新 更多