0
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json()); 
app.set('view engine', 'ejs');

<form action='/login' method = "GET">
    <div class="input-field">
        <input type="text" name = "username" placeholder="Enter your Username" value = "asdasdas" required />
        <i class="uil uil-dialpad-alt icon"></i>
    </div>
    <div class="input-field">
        <input type="password" class="password" placeholder="Enter Password" name = "password_login" value = "asdasdas" required  />
       <i class="uil uil-lock icon"></i>
       <i class="uil uil-eye-slash eye"></i>
   </div>
   <div class="input-field button">
       <input type="submit" value="Login"/>
   </div>
</form>


app.get('/login', (req, res)=>{
    console.log (req.body.username);
    console.log(req.body.password_login);
    res.send("put tank in a mall");
});

Here is my code in the server and the form looks like this

Why is the req returning undefined while everything is fine when I'm using my other forms? I have been stuck here for the past 2 hours pls send help

  • Is the form action attribute a GET or POST? Shows GET in the code provided but should it be a POST? – MZM Nov 25 '22 at 16:36
  • i've tried changing the app to app.post and the method to post and still says that cannot get/login – PP GODKING Nov 25 '22 at 16:40
  • Show the index.js you're using or see the link below for getting started with 'body-parser' sample app, https://www.geeksforgeeks.org/body-parser-middleware-in-node-js/ – MZM Nov 25 '22 at 16:56
  • https://jsfiddle.net/curses520/6bk8rj2z/#&togetherjs=ADcauG1mR5 here is a link to what my server looks like. I'm using routes i don't know if that is causing it. Just to let you know the EJS also has 2 forms. – PP GODKING Nov 25 '22 at 17:05
  • Does changing the urlencoded to true or false changes anything? – PP GODKING Nov 25 '22 at 17:15
  • You'll find full answer in the following post, https://stackoverflow.com/questions/24543847/req-body-empty-on-posts?rq=1. – MZM Nov 25 '22 at 17:17
  • do console.log(console.log (req.body) and provide output. Check if form fields present in there? – Brijesh Dave Nov 26 '22 at 04:27

1 Answers1

0

The issue with the request method over here you are using GET method instead of POST to send data

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json()); 
app.set('view engine', 'ejs');

<form action='/login' method = "POST">
    <div class="input-field">
        <input type="text" name = "username" placeholder="Enter your Username" value = "asdasdas" required />
        <i class="uil uil-dialpad-alt icon"></i>
    </div>
    <div class="input-field">
        <input type="password" class="password" placeholder="Enter Password" name = "password_login" value = "asdasdas" required  />
       <i class="uil uil-lock icon"></i>
       <i class="uil uil-eye-slash eye"></i>
   </div>
   <div class="input-field button">
       <input type="submit" value="Login"/>
   </div>
</form>


app.post('/login', (req, res)=>{
    console.log (req.body.username);
    console.log(req.body.password_login);
    res.send("put tank in a mall");
});

Now this will works

Smit Gajera
  • 1,001
  • 1
  • 8
  • 26