I am using a mongoDB where each object (I think of this as a row) has about 30 items (I think of the items as columns), I want to use an update function so that just a single item in each object is updated when its called whilst keeping the rest the same. Below is the code I am using.
const handleTeamUpdate = (id, column, value) => {
const obj = {
[column]: value
};
axios
.post('http://localhost:4000/todos/update/' + id, obj)
.then(() => {
console.log(`Team updated`)
fetchTeams() // refresh the teams
})
.catch(error => console.error(`There was an error updating.`))
}
todoRoutes.route('/update/:id').post(function (req, res) {
Todo.findById(req.params.id, function (err, todo) {
if (!todo)
res.status(404).send("data is not found");
else
todo.Team_Name = req.body.Team_Name;
todo.Team_Manager = req.body.Team_Manager;
todo.Department = req.body.Department;
todo.Location = req.body.Location;
// 26 more columns below the above
todo.save().then(todo => {
res.json('Todo updated!');
})
.catch(err => {
res.status(400).send("Update not possible");
});
});
});
What my current code seems to do is update the item I specify (I refer to this as column in my code) but then seems to delete/nullify every other 'column' - I am looking for a solution which updates the column I specify but also keeps every other column the same - I would prefer if I don't have to state each column as an argument in my Update function as it will make the code much more messy.