Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
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);
            });
    }
about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!