'400 Bad request when trying to post to database
I'm putting together a simple todo list app using the MERN stack. My create-todo function suddenly stopped working, getting the console error of 400 bad request. I don't remember making any changes to that part of the code, it started after adding user registration/login. Here's my code:
Controller
Todo.create(req.body)
.then((data) => {
console.log({ data });
res.json({ message: "todo added successfully", data });
})
.catch((err) =>
res.status(400).json({
message: "unable to add new todo",
error: err.message,
})
);
};
end point
/**
* @route PUT api/todo/:id
* @description update todo
* @access public
*/
front-end
const [data, setData] = useState({ title: "", description: "" });
function handleChange(e) {
setData((data) => ({ ...data, [e.target.name]: e.target.value }));
}
function handleSubmit(e) {
e.preventDefault();
axios
.post("http://localhost:8000/api/todo", data)
.then((res) => {
setData({ title: "", description: "" });
console.log(res.data.message);
})
.catch((err) => {
console.log("Error couldn't create TODO");
console.log(err.message);
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
