我有一个 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.markModified('lines')。我将代码添加到原始帖子中。添加此操作可以使模型正确保存。
关于node.js - Mongoose 模型在数组拼接后不保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23047414/