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

Time:11-08

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

CodePudding user response:

add this to file where define express after define express

app.use(express.json());
  • Related