Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
Express server throws weird "cannot read properties of undefined reading "name" error.."

I'm trying to create a register route but the post request return cannot read properties of undefined. reading 'name'. And the "existUser" always returns true even though theres no user with the email in the database. Is there something that i'm missing here ? heres my route -

router.post('/register', async (req, res) => {
    try {
        const user = new User({
            name: req.body.name,
            email: req.body.email,
            password: crypto.AES.encrypt(req.body.password, process.env.CRYPTO_SEC).toString(),
            dateOfBirth: req.body.dateOfBirth,
        });
        const existUser = await User.findOne({ email: user.email });
        if (existUser) {
            return res.status(400).json({ msg: "Email Already Exist!" });
        } else {
            try {
                const newUser = await user.save();
                res.status(200).json(newUser);
            } catch (err) {
                res.status(400).json({ msg: err.message });
            }
        }
    } catch (err) {
        res.status(400).json({ msg: "An Error Occured!" })
    }
});

User model -

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    dateOfBirth: {
        type: Date,
        required: true
    },
    profilePicture: {
        type: String,
        required: false,
        default: ""
    },
});

module.exports = mongoose.model("Users", userSchema);

How do I solve this weird error.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

probably the request body property is not being found.
confirm you are passing some middleware from bodyparser to express.

something like that:

const express = require('express');
const app = express();

app.use(express.json());
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!