Sai Krishnadas
Sai Krishnadas

Reputation: 3419

variable is not defined during the form post request

I need to send the values through the html form and receive it in the console.log. But once i send the data, the error pops-up "TypeError: Cannot read property 'fName' of undefined" even though i have declared it in the form name.

signin.html :

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
    <meta name="generator" content="Hugo 0.80.0">
    <title>Signin Template · Bootstrap v5.0</title>

    <link rel="canonical" href="https://getbootstrap.com/docs/5.0/examples/sign-in/">



    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">


    <!-- Favicons -->
<link rel="apple-touch-icon" href="/docs/5.0/assets/img/favicons/apple-touch-icon.png" sizes="180x180">
<link rel="icon" href="/docs/5.0/assets/img/favicons/favicon-32x32.png" sizes="32x32" type="image/png">
<link rel="icon" href="/docs/5.0/assets/img/favicons/favicon-16x16.png" sizes="16x16" type="image/png">
<link rel="manifest" href="/docs/5.0/assets/img/favicons/manifest.json">
<link rel="mask-icon" href="/docs/5.0/assets/img/favicons/safari-pinned-tab.svg" color="#7952b3">
<link rel="icon" href="/docs/5.0/assets/img/favicons/favicon.ico">
<meta name="theme-color" content="#7952b3">


    <style>
      .bd-placeholder-img {
        font-size: 1.125rem;
        text-anchor: middle;
        -webkit-user-select: none;
        -moz-user-select: none;
        user-select: none;
      }

      @media (min-width: 768px) {
        .bd-placeholder-img-lg {
          font-size: 3.5rem;
        }
      }
    </style>


    <!-- Custom styles for this template -->
    <link href="css/styles.css" rel="stylesheet">
  </head>
  <body class="text-center">

<main class="form-signin">
  <form action="/" method="POST">

    <img class="mb-4" src="images/logo.jpg" alt="" width="72" height="57">
    <h1 class="h3 mb-3 fw-normal">Sign Up Here</h1>
    <input type="text" name ="fName" class="form-control top" placeholder="First Name" required autofocus>
    <input type="text" name ="lName" class="form-control middle" placeholder="Last Name" required>
    <input type="email" name ="email" class="form-control bottom" placeholder="Email" required>

    <button class="w-100 btn btn-lg btn-primary signinbutton" type="submit">Sign Up!</button>
    <p class="mt-5 mb-3 text-muted">&copy; The ThunderStorm</p>
  </form>
</main>



  </body>
</html>

app.js:

//jshint esversion:6

const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');

const app = express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));

app.get('/', function(req, res){

  res.sendFile(__dirname + "/signup.html");
});

app.post("/",function(req,res){

  var firstName = res.body.fName;
  var lastName = res.body.lName;
  var email = res.body.email;

  console.log(firstName,lastName,email);
});




app.listen(3000,function(){
  console.log("Started listening on port 3000");
});

error log:

TypeError: Cannot read property 'fName' of undefined at G:\WebDevelopmentFolder\Newsletter-Signup\app.js:18:28 at Layer.handle [as handle_request] (G:\WebDevelopmentFolder\Newsletter-Signup\node_modules\express\lib\router\layer.js:95:5) at next (G:\WebDevelopmentFolder\Newsletter-Signup\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (G:\WebDevelopmentFolder\Newsletter-Signup\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (G:\WebDevelopmentFolder\Newsletter-Signup\node_modules\express\lib\router\layer.js:95:5) at G:\WebDevelopmentFolder\Newsletter-Signup\node_modules\express\lib\router\index.js:281:22 at Function.process_params (G:\WebDevelopmentFolder\Newsletter-Signup\node_modules\express\lib\router\index.js:335:12) at next (G:\WebDevelopmentFolder\Newsletter-Signup\node_modules\express\lib\router\index.js:275:10) at G:\WebDevelopmentFolder\Newsletter-Signup\node_modules\body-parser\lib\read.js:130:5 at invokeCallback (G:\WebDevelopmentFolder\Newsletter-Signup\node_modules\raw-body\index.js:224:16)

Upvotes: 1

Views: 293

Answers (1)

Arun Kumar Mohan
Arun Kumar Mohan

Reputation: 11915

You need to read the body from the request object.

const firstName = req.body.fName;
const lastName = req.body.lName;
const email = req.body.email;

Upvotes: 3

Related Questions