I'm using MongoDB with mongoose, and my document is looking like this:
{
name: "bob"
employees: [
{
...,
id: "__sdab234"
},
{
...,
id: "__wqerew544"
}
]
patients: [
{
...,
employeeSelected: "__sdab234"(id of employee)
}
I seem to be able to push to patients array, but not add or change any data of an existing object inside the array.
My code:
User.findOne({ _id: id }, function(err, user) {
if (!user || user === null) res.sendStatus(403);
const patientFound = user.patients.filter(
patient => patient.id === idPatientSent
);
//check if found
if (patientFound.length === 0) return res.sendStatus(401);
patientFound[0].employeeSelected = idEmployeeSent;
user.save();
console.log(user.patients)
//send employee to client
res.status(200).send({
message: "Successfully updated selected employee!"
});
});
It seems like the changes are made, but does not persist.