'TypeError: Cannot read properties of undefined (reading 'email')

I'm trying to authenticate user. Sign-up is working fine but in sign-in req.body.email is undefined and I can't figure out why. Here is my code for sign-in:

router.post('/signin',(res,req,next)=>{
    User.findOne({ email: req.body.email })
    .exec()
    .then(user => {
        if(user.length<1){
            return res.status(401).json({
                message: 'Auth Failed'
            })
        }
        bcrypt.compare(req.body.password, this.user[0].password, (err,result)=>{
            if(err){
                return res.status(401).json({
                    message: 'Auth Failed'
                });
            }
            if(result){
                return res.status(200).json({
                    message: 'Auth Successful'
                });
            }
            res.status(401).json({
                message: 'Auth Failed'
            });
        })
    })
    .catch(err => {
        console.log(err);
        res.status(500).json({
            error: err
        });
    })
    
})

On Postman:

{
    "email": "[email protected]",
    "password": "12345"
}

Console Error:

TypeError: Cannot read properties of undefined (reading 'email')
    at C:\Users\SJPC\auth\routes\user.route.js:62:37
    at Layer.handle [as handle_request] (C:\Users\SJPC\auth\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\SJPC\auth\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\SJPC\auth\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\SJPC\auth\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\SJPC\auth\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\SJPC\auth\node_modules\express\lib\router\index.js:341:12)
    at next (C:\Users\SJPC\auth\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\Users\SJPC\auth\node_modules\express\lib\router\index.js:174:3)
    at router (C:\Users\SJPC\auth\node_modules\express\lib\router\index.js:47:12)

I have tried req.query.email but it also thrown the same error.



Solution 1:[1]

You should add this code

app.use(express.json())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jakub Kurdziel