Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot read properties of undefined problem in postman

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}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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}));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading