1

I am working with Nodejs(Express.js), I am sending form data to "controller" but I am getting folllowing error

TypeError: Cannot read property 'name' of undefined

Here is my controller code,My req.body.name is not working

userController.save = function (req, res, next) {
    var name = req.body.name;
    console.log(name);   
}

Here is my app.js, I am using MVC structure in nodejs

const express = require('express');
const mysql = require('mysql');
const expressValidator = require('express-validator');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const flash = require('connect-flash');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/', routes);
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(__dirname + '/public'));
app.use(cookieParser('keyboard cat'));
const PORT = 4000
app.listen(PORT, function (err) {
  if (err) console.log(err);
  console.log("App started on PORT", PORT);
});

Here is my view file code(signup.pug)

 form#myForm.my_form.pt-3(action='', method='POST')
     .form-group
         .row
             .col-md-3
                  label(for='Name') Name
             .col-md-9
                  input#name.form-control(type='text', name='name', placeholder='Enter your name')
     .form-group
         .row.pt-2
              .col-md-12
                  input#submit.btn.btn-dark.form-control(type='submit', name='submit', value='Register')
Rahul Kadukar
  • 858
  • 3
  • 15
  • 35
amit
  • 1
  • 1
  • 18
  • 28

1 Answers1

0

Although lacking of information, I believe your app.use('/', routes) must be put after the view engine, file path etc.

    const express = require('express');
    const mysql = require('mysql');
    const expressValidator = require('express-validator');
    const bodyParser = require('body-parser');
    const cookieParser = require('cookie-parser');
    const session = require('express-session');
    const flash = require('connect-flash');
    const app = express();
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(bodyParser.json());

    app.set('view engine', 'pug');
    app.set('views', path.join(__dirname, 'views'));
    app.use(express.static(__dirname + '/public'));
    app.use(cookieParser('keyboard cat'));

    app.use('/', routes);

    const PORT = 4000

    app.listen(PORT, function (err) {
      if (err) console.log(err);
      console.log("App started on PORT", PORT);
    });
Titan XP
  • 399
  • 3
  • 11
  • Actually "Form Data" is getting in "app.js" file but i want to pass data in "UserController" instead of "app.js",Because i want to use MVC structure,Hope you understand my point – amit Aug 04 '22 at 04:38
  • Not sure about your routes but if you try this const userController = function (req, res, next). See whether it works or not. Thanks – Titan XP Aug 04 '22 at 04:48
  • add new question and explained more https://stackoverflow.com/questions/73230487/how-to-pass-data-controller-instead-of-app-js-in-nodejs – amit Aug 04 '22 at 04:52