title: TypeError: Cannot destructure property 'username' of 'request.body'
body: <p>I am declaring the variables in router.post and retrieving the data from the database but it says a type error where it cannot destructure property 'username' of 'request.body' as it is undefined. Please advice.</p>
  <p>This is the folder with issue at <code>const { username, password } = request.body;</code>.</p>
  <p>User.js (under routes folder)</p>
  <pre><code>const express = require(&quot;express&quot;);
  const router = express.Router();
  const mysql = require(&quot;mysql&quot;);
  
  //connection to database
  const connection = mysql.createConnection({
    host: process.env.DATABASE_HOST,
    user: process.env.DATABASE_USER,
    password: process.env.DATABASE_PASSWORD,
    database: process.env.DATABASE,
  });
  
  router.get(&quot;/&quot;, function (request, response) {
    response.render(&quot;login.html&quot;);
  });
  
  router.post(&quot;/auth&quot;, function (request, response) {
  
    try {
      const { username, password } = request.body;
      connection.query(
        &quot;SELECT * FROM accounts WHERE username = ? AND password = ?&quot;,
        [username, password],
        function (error, results, fields) {
          if (results.length &gt; 0) {
            request.session.loggedin = true;
            request.session.username = username;
            response.redirect(&quot;/agenthome&quot;);
          } else {
            response.send(&quot;Incorrect Username and/or Password!&quot;);
          }
          response.end();
        }
      );
    } catch (error) {
      console.log(error);
    }
  });
  
  module.exports = router;
  </code></pre>
  <p>app.js</p>
  <pre><code>const mysql = require(&quot;mysql&quot;);
  const express = require(&quot;express&quot;);
  const session = require(&quot;express-session&quot;);
  const bodyParser = require(&quot;body-parser&quot;);
  const dotenv = require(&quot;dotenv&quot;);
  const path = require('path');
  const app = express();
  
  const userRoutes= require('./routes/user');
  
  //env configuration
  dotenv.config({ path: &quot;./.env&quot; });
  
  app.use(&quot;/&quot;, userRoutes);
  
  //connection to database
  const connection = mysql.createConnection({
    host: process.env.DATABASE_HOST,
    user: process.env.DATABASE_USER,
    password: process.env.DATABASE_PASSWORD,
    database: process.env.DATABASE,
  });
  
  // check database connection err
  connection.connect(function (err) {
    if (err) {
      return console.error(&quot;error: &quot; + err.message);
    }
    console.log(&quot;Connected to the MySQL server.&quot;);
  });
  
  //prevent course errors
  app.use((res, req, next) =&gt; {
    res.header(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;);
    res.header(
      &quot;Access-Control-Allow-Headers&quot;,
      &quot;Origin, X-Requested-With, Content-Type, Accept, Auhtorization&quot;
    );
    if (req.method === &quot;OPTIONS&quot;) {
      res.header(&quot;Access-Control-Allow-Methods&quot;, &quot;PUT, POST, PATCH, DELETE, GET&quot;);
    }
    next();
  });
  
  app.use(
    session({
      secret: &quot;secret&quot;,
      resave: true,
      saveUninitialized: true,
    })
  );
  app.use('/assets', express.static('assets')); 
  
  //view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set(&quot;view engine&quot;, &quot;ejs&quot;);
  
  //render HTML files
  app.engine('html', require('ejs').renderFile);
  
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(bodyParser.json());
  
  app.listen(3000);
  
  </code></pre>