我不明白为什么无法重新分配Mongoose find
结果引用。
例如
Ticket.findOne({_id: req.params.id}, (err, ticket) => {
console.log("before: ", ticket)
ticket.tasks = [1, 2] //won't work
ticket.title = "foo"
console.log("after: ", ticket)
console.log("=========")
let copyTicket = ticket.toObject()
console.log("Solution after: ", copyTicket)
copyTicket.tasks = [1, 2]
copyTicket.title = "bar"
console.log("Solution before: ", copyTicket)
})
输出
before: { tasks: [],
_id: 5aa17051e97eac407b72757a,
title: 'ho'
}
after: { tasks: [],
_id: 5aa17051e97eac407b72757a,
title: 'foo',
}
=========
Solution after: { tasks: [],
_id: 5aa17051e97eac407b72757a,
title: 'foo',
}
Solution before: { tasks: [ 1, 2 ],
_id: 5aa17051e97eac407b72757a,
title: 'bar',
}
您是否有任何想法如何以及为何会出现这种行为?