Cannot read properties of undefined problem in postman

Advertisements

I am tring to create a user in my database by letting user signup.

when i enter the data manualy in create method it works, like that

note: the schema componants is {userName:String , number:String , email:String , Password:String}

exports.Signup = catchAsync(async (req, res, next) => {
  const newUser = await User.create({
    userName: 'YoussefAdel',
    number: '12345678948',
    email: 'youssef@gmail.com',
    password: '123456789',
  });

  res.status(201).json({
    status: 'success',
    data: {
      user: newUser,
    },
  });
});

But when i try to take the data from the req.body comming from postman the data is readed as undefined

exports.Signup = catchAsync(async (req, res, next) => {
  const newUser = await User.create(
  {   userName: req.body.userName,
      number: req.body.number,
      email: req.body.email,
      password: req.body.password
  });

  res.status(201).json({
    status: 'success',
    data: {
      user: newUser,
    },
  });
});

and this is the error in postman
Cannot read properties of undefined (reading 'userName')

i really need help as iam stuck in this problem for 2 days

i tried to replace those lines in the signup method

      userName: req.body.userName,
      number: req.body.number,
      email: req.body.email,
      password: req.body.password

with req.body
and the problem is still the same

this is the json code that i post the request with through postman

{
    "userName" : "YoussefAdel",
    "number" : "12345678948",
    "email" : "youssef@gmail.com",
    "password" : "123456789"
}

>Solution :

body-parser was marked as deprecated.

Try use:

// JSON
app.use(express.json());
// x-www-form-urlencoded
app.use(express.urlencode({extended: false}));

Leave a Reply Cancel reply