skip to Main Content

Whenever I POST to the API it shows this error:

This is my code in userController.js

const registerUser = asyncHandler(async (req, res) => {
    const {name, email, password, phoneNumber} = req.body

    if(!name || !email || !password || !phoneNumber) {
        res.status(400)
        throw new Error('Please add all fields')
    }
    res.json({ message: 'Register user'})
})

This is my code from the userModel:

2

Answers


  1. Chosen as BEST ANSWER

    I have a simple CRUD in Node JS and use Mongoose. Why is postman still showing a 400 bad request when POSTing?

    It is working now when sending thru the 'Body'.

    Postman Screenshot


  2. You’re passing in query params in Postman, but in the controller you are accessing the body.

    Change one or the other, either by passing in form data in the body of Postman, or by accessing req.params in your controller.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search