It's been a couple of hours and I'm still stuck on this issue >.<
I'm having trouble accessing the request body for the user API. While I can retrieve the response 'hi' using res.send (as shown in the screenshot), I'm not able to capture any req.body values. The email is returning as undefined :/
Any thoughts on where I might be making a mistake?
The content type is set to JSON in 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
https://i.sstatic.net/ooBJl.png
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: https://i.sstatic.net/pVUaZ.png