title: TypeError: Cannot read property 'password' of undefined
body: <p>I am learning Node Js and express as ORM i need to create user authorization and i have managed to create signup backend code and tested it via postman and it work perfect, the challenge come with login, when i test it via postman i get an this error in my vscode terminal "TypeError: Cannot read property 'password' of undefined" I have tried to find out but many solution state there is a problem with sequelize order, that app.use(bodyParser.json()); should come first before routes and my order are correct but still a get the same error. please who can help me</p>
  
  <p>My user (signup and login code)</p>
  
  <pre><code>const express = require('express');
  const bcrypt = require('bcrypt');
  const User = require('../models/users');
  const router = express.Router();
  router.post('/signup',(req, res, next)=&gt;{
      User.findOne({
          where:{
              email:req.body.email
          }
      }).then(user=&gt;{
          if(user){
              res.status(409).json({
                  message:'Email alreadly exist!'
              })
          }else{
              bcrypt.hash(req.body.password, 10, (err, hash)=&gt;{
                  if(err){
                      return res.status(500).json({
                          error:err
                      });
                  }else{
                      const user =new User({
                          email:req.body.email,
                          password: hash
                      });
                      user.save()
                      .then(result=&gt;{
                          // console.log(result)
                          res.status(201).json({
                              message:'User created successfully'
                          });
                      })
                      .catch(err=&gt;{
                          console.log(err);
                          res.status(500).json({
                              error:err
                          });
                      });
                  }
              })
          }
      })
  
  });
  
  router.post('/login',(req, res, next)=&gt;{
      User.findOne({
          where:{
              email:req.body.email
          }
      })
      .then(user =&gt;{
          if(user.length&lt;1){
              return res.status(401).json({
                  message:'Authentication failed'
              });
          }
          bcrypt.compare(req.body.password, user[0].password, (err, result)=&gt;{
              if(err){
                  return res.status(401).json({
                      message:'Authentication failed'
                  });
              }
              if(result){
                  return res.status(200).json({
                      message:'Authorization granted!'
                  });
              }
              return res.status(401).json({
                  message:'Authentication failed'
              });
          });
      })
      .catch(err=&gt;{
          console.log(err);
          res.status(500).json({
              error:err
          });
      });
  });
  module.exports=router;
  </code></pre>
  
  <p>my app.js code</p>
  
  <pre><code>const express = require('express');
  const app= express();
  const bodyParser = require('body-parser');
  
  app.use(bodyParser.urlencoded({extended:true}));
  app.use(bodyParser.json());
  const productRoutes = require('./api/routes/products');
  const orderRoutes = require('./api/routes/orders');
  const userRoutes =require('./api/routes/users');
  const sequelize =require('./api/config/database');
  
  
  
  
  
  app.use((req, res, next)=&gt;{
      res.header('Access-Control-Allow-Origin','*');
      res.header('Access-Control-Allow-Headers','Origin, X-Requested, Content-Type, Accept, Authorization');
  
          if(req.method === 'OPTIONS'){
              res.header('Access-Control-Allow-Methods','PUT, POST, GET, DELETE');
              return res.status(200).json({
  
              });
          }
          next();//this allow our request to procede
  });
  
  
  app.use('/products',productRoutes);
  app.use('/orders',orderRoutes);
  app.use('/users',userRoutes);
  
  app.use((req, res, next)=&gt;{
      const error =new Error('Page Not Found');
      error.status=404;
      next(error);
  });
  
  app.use((error, req, res, next)=&gt;{
      res.status(error.status||500);
      res.json({
          error:{
              message:error.message
          }
      });
  });
  sequelize.sync().then(result=&gt;{
      // console.log(result);
  })
  .catch(err=&gt;{
      console.log(err);
  });
  module.exports=app;
  </code></pre>
  
  <p>this is pc from postman<a href="https://i.stack.imgur.com/vEpzR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vEpzR.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/PLTx2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PLTx2.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/EnnwX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EnnwX.png" alt="enter image description here"></a></p>
  
  <pre><code>Register and login routes
  
  const express = require('express');
  const bcrypt = require('bcrypt');
  const User = require('../models/users');
  const router = express.Router();
  router.post('/signup',(req, res, next)=&gt;{
      User.findOne({
          where:{
              email:req.body.email
          }
      }).then(user=&gt;{
          if(user){
              res.status(409).json({
                  message:'Email alreadly exist!'
              })
          }else{
              bcrypt.hash(req.body.password, 10, (err, hash)=&gt;{
                  if(err){
                      return res.status(500).json({
                          error:err
                      });
                  }else{
                      const user =new User({
                          email:req.body.email,
                          password: hash
                      });
                      user.save()
                      .then(result=&gt;{
                          // console.log(result)
                          res.status(201).json({
                              message:'User created successfully'
                          });
                      })
                      .catch(err=&gt;{
                          console.log(err);
                          res.status(500).json({
                              error:err
                          });
                      });
                  }
              })
          }
      })
  
  });
  
  
  router.post('/login', async (req, res, next) =&gt; {
  
  
      console.log("req.body: ", req.body);
  
      try {
          if (!req.body.email || !req.body.password) {
              console.log("Email and password not given");
              return res.status(400).send("Email and password is required");
          }
  
          const { email, password } = req.body;
  
          let user = await User.findOne({ email });
  
          if (!user) {
              console.log("user not found");
              return res.status(401).json({ message: 'Authentication failed' });
          }
  
          const validPassword = await bcrypt.compare(password, user.password);
  
          if (!validPassword) {
              console.log("Password is not valid");
              return res.status(401).json({ message: 'Authentication failed' });
          }
  
          return res.status(200).json({ message: 'Authorization granted!' });
      }
      catch (err) {
          console.log("Err: ", err);
          res.status(500).json({ error: err });
      }
  
  });
  module.exports=router;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/NS7FL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NS7FL.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/jvoeN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jvoeN.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/XUzi5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XUzi5.png" alt="enter image description here"></a></p>
  
  <p>this is my db, model and server</p>
  
  <pre><code>const http = require('http');
  const app = require('./app');
  const PORT = process.env.PORT||4000;
  const server =http.createServer(app);
  server.listen(PORT);
  </code></pre>
  
  <p>user model</p>
  
  <pre><code>const Sequelize = require('sequelize');
  const sequelize = require('../config/database');
  const User = sequelize.define('users',{
      id:{
          type: Sequelize.INTEGER,
          autoIncrement: true,
          allowNull: false,
          primaryKey:true
      },
      email:{
          type: Sequelize.STRING(50),
          required: true,
          notEmpty:true,
          validate:{
              isEmail:true,
          },
          unique:'email'
  
      },
      password:{
          type: Sequelize.STRING(30),
          required: true
      }
  
  });
  
  module.exports=User;
  </code></pre>
  
  <p>db </p>
  
  <pre><code> const Sequelize = require('sequelize');
      const sequelize =new Sequelize('projectone','root','',{
          dialect: 'mysql',
          host: 'localhost'
      });
  
  
      module.exports=sequelize;
  </code></pre>
  
  <p>this is user routes for signup and login</p>
  
  <pre><code>const express = require('express');
  const bcrypt = require('bcrypt');
  const User = require('../models/users');
  const router = express.Router();
  router.post('/signup', async (req, res, next) =&gt; {
  
      try {
        const { email, password } = req.body;
  
        let user = await User.findOne({where:{email}});
  
        if (user) return res.status(409).json({ message: 'Email already exist!' });
  
        user = new User({ email, password });
        const salt = await bcrypt.genSalt(10);
        user.password = await bcrypt.hash(user.password, salt);
        await user.save();
  
        res.status(201).json({ message: 'User created successfully' });
      }
      catch (err) {
        console.log(err);
        res.status(500).json({
          error: err
        });
      }
    });
  
    router.post("/login", async (req, res, next) =&gt; {
  
      try {
        if (!req.body.email || !req.body.password) {
          return res.status(400).send("Email and password is required");
        }
  
        const { email, password } = req.body;
  
        let user = await User.findOne({ where: { email } });
  
        if (!user) {
          console.log("user not found");
          return res.status(401).json({ message: "Authentication failed" });
        }
  
        const validPassword = await bcrypt.compare(password, user.get("password"));
  
        if (!validPassword) {
          console.log("Password is not valid");
          return res.status(401).json({ message: "Authentication failed" });
        }
  
        return res.status(200).json({ message: "Authorization granted!" });
      } catch (err) {
        console.log("Err: ", err);
        res.status(500).json({ error: err });
      }
    });
  
  module.exports=router;
  </code></pre>