【问题标题】:Mongoose model not saving after array splice数组拼接后猫鼬模型不保存
【发布时间】:2014-05-27 15:56:59
【问题描述】:

我有一个 Mongoose 模型,它的数组属性具有嵌套数组属性。以下代码中的 console.logs 准确地显示了我期望的数据,但是,数据没有保存到数据库中,我不知道为什么......

self.findByCustomerId(customerId, function (err, order) {
    if (err) done(err, null);
    console.log('Toppings before splice: ',order.lines[lineIdx].toppings);
    order.lines[lineIdx].toppings.splice(toppingIdx, 1);
    order.updated = new Date();

    // NOTE: This line causes the order.save to work
    order.markModified('lines');

    order.save(function (err) {
        if (err) done(err, null);
        console.log('Toppings after splice: ',order.lines[lineIdx].toppings);
        done(null,"OK");
    });
});

所以除了最后的数据库持久性之外,一切都运行良好。我错过了什么?

谢谢你的帮助,我迷路了。

【问题讨论】:

  • 您能否更新您的问题以包含 order 的架构?

标签: node.js mongodb mongoose


【解决方案1】:

它需要 order.markModified('lines')。我将代码添加到原始帖子中。添加它会导致模型正确保存。

【讨论】:

    猜你喜欢
    • 2016-06-29
    • 2020-09-05
    • 2021-06-04
    • 2021-12-04
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    相关资源
    最近更新 更多