I don't understand why Mongoose find
result references cannot be reassigned.
eg:
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)
})
Output
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',
}
Do you have any ideas how and why there is this behavior?