I am trying to update the favourite stations array in my database, however I just can't seem to be able to update it? I have searched for ages to try and find a solution, but nothing seems to work.
I have the ID saved in the cookie, and am sending that in the body when the update request is made...
"selected" is an array of objects. this is what will need replace the contents of the to the favouriteStations property, or add it if it is empty.
the database structure is like this:
"id": id,
"email: "test@test",
"password": "test",
"userInfo": {
"firstName": "test",
"lastName": "test",
"favouriteStations": [{array i want to replace}]
}
i have tried many different combinations, however this is what i have so far, and it doesnt work
app.post("/api/update", (req, res) => {
console.log("updating user details");
const { id, selected } = req.body;
UserModel.findOneAndUpdate(
{ _id: id },
{ userInfo: { favouriteStations: { $set: selected } } },
{ useFindAndModify: false }
)
.then((user, err) => {
if (err) {
console.log("an error?");
res.status(500).json({
error: "Server error",
});
} else if (!user) {
console.log("user not exists?");
res.status(401).json({
error: "Incorrect credentials",
});
} else {
console.log("updated?");
console.log(user);
}
})
.catch((err) => {
console.log(err);
});
});