'How I am gonna solve req.body.password undefined in express.js?
Hello devs I am building an API using EXPRESS.JS AND MongoDB and performing some tests with postman. But I am getting the req. body. password field is undefined when I try to parse it using postman however I am able to parse the name and the email but the password is undefined. I need help !
bellow are my files
This is my userController.js file
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');
const asyncHandler = require('express-async-handler');
const User = require('../models/userModel');
// @Desc Register New User
// @Routes POST /api/users
// @Access Public
const userRegister = asyncHandler(async (req, res) => {
const {name, email, password} = req.body;
res.json({name: `${name}`, email: `${email}`, password: `${password}`});
if (!name || !email) {
res.status(400);
throw new Error('Please Enter valid Inputs');
}
res.json({ message: 'User Register' });
});
// @Desc Authenticate User
// @Routes POST /api/users/login
// @Access Public
const userLogin = asyncHandler(async (req, res) => {
res.json({ message: 'User Login' });
});
// @Desc User Data
// @Routes POST /api/users/member
// @Access Public
const userMember = asyncHandler(async (req, res) => {
res.json({ message: 'User Member' });
});
// @Desc User Data
// @Routes GET /api/users
// @Access Public
const userProfile = asyncHandler(async (req, res) => {
res.json({ message: 'User Profile' });
});
module.exports = {
userRegister,
userLogin,
userMember,
userProfile,
};
And here is my server.js
const express = require('express');
const colors = require('colors');
const dotenv = require('dotenv').config();
const { errorHandler } = require('./middleware/errorMiddleware');
const connectDB = require('./config/db');
const port = process.env.PORT || 8000;
connectDB();
const app = express();
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Routes
const postRoute = require('./routes/postRoutes');
const userRoute = require('./routes/userRoutes');
app.use('/api/posts', postRoute);
app.use('/api/users', userRoute);
app.use(errorHandler);
app.listen(port, () => console.log(`Server running on http://localhost:${port}`));
The route file is
const express = require('express');
const router = express.Router();
// import User controllers
const {
userRegister,
userLogin,
userMember,
userProfile,
} = require('../controllers/userController');
router.route('/').post(userRegister);
router.route('/member').post(userMember);
router.route('/login').post(userLogin);
router.route('/profile').get(userProfile);
module.exports = router;
Here is the userModel.js file
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
unique: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
profilePicture: {
type: String,
default: '',
},
},
{ timestamps: true }
);
// Exporting the schema
module.exports = mongoose.model('User', userSchema);
And finally, this is the output from POSTMAN
Solution 1:[1]
I was able to solve it just by changing this
const {name, email, password} = req.body;
to
const {name, email, pass} = req.body;
Updating password to pass
This change makes it work for me
Thank you so much for your support
Blockquote
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


