Can't retrieve req body from api – undefined

Been stuck on this over an hour or two now >.<

I am unable to access the request body for the user api. I can get the response ‘hi’ back using res.send (see screenshot) but not with any req.body values. Email is coming back undefined :/

Any ideas where I am going wrong?

Content type is set to json on the postman headers.

Controller:

import asyncHandler from 'express-async-handler'

const authUser = asyncHandler(async (req, res) => {
    const { email, password } = req.body
  
    res.send('hi' + email)
    
})

export {authUser}

Route:

import express from 'express'
const router = express.Router()
import { authUser } from '../controllers/userController.js'

router.post('/login', authUser)

export default router

enter image description here

hiundefined ^

Server.js

import express from 'express'
import dotenv from 'dotenv'
import connectDB from './config/db.js'
import productRoutes from './routes/productRoutes.js'
import userRoutes from './routes/userRoutes.js'
import colours from 'colours'
import { notFound, errorHandler} from './middleware/errorMiddleware.js'

dotenv.config()

connectDB()

const app = express()

app.use(express.json())

app.get('/', (req, res) => {
    res.send('API is running...')
})

app.use('/api/products/', productRoutes)
app.use('/api/users/', userRoutes)

app.use(notFound)
app.use(errorHandler)

const PORT = process.env.PORT || 5000

app.listen(
    PORT,
    console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`.yellow.bold)
)

Headers:
enter image description here

24 thoughts on “Can't retrieve req body from api – undefined”

Leave a Comment