i got this error TypeError: Cannot read properties of undefined (reading 'username')

Issue

i’m following a tutorial and i want to do a register functionnality
but i got this error :
TypeError: Cannot read properties of undefined (reading ‘username’)

here is the auth file :

const router = require('express').Router();
const User = require("../models/User")
router.post("/register", async (req,res)=>{
    const newUser = new User({
        username:req.body.username,
        password:req.body.password,
        email:req.body.email,
    });
    try {
        const user = await newUser.save();
        res.json(user)
    }
    catch(err){
        res.status(500).json(err)
    }
 
})
module.exports = router

Solution

add this to file where define express after define express

app.use(express.json());

Answered By – mohammad Naimi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published