Home > OS >  I have a simple CRUD in Node JS and use Mongoose, why is postman still showing a 400 bad request whe
I have a simple CRUD in Node JS and use Mongoose, why is postman still showing a 400 bad request whe

Time:07-05

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:

CodePudding user response:

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.

CodePudding user response:

It is working now when sending thru the 'Body' WORKING: It seems that it won't work when sending thru 'Params' but is working in 'Body'

  • Related