Node/Express JS "Cannot read property 'username' of undefined"

10,018

Solution 1

My bet is that your call to routes is happening before your app.use is calling bodyparser. Try moving your routes below them.

Change

var routes = require('./server/routes');   
routes(app);
//...
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

To

var routes = require('./server/routes');
//...
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//...
routes(app);

Solution 2

var routes = require('./server/routes');

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
 //...
routes(app);

Everything should be inside the route. If not, you will have undefined error.

Share:
10,018
Isaac Krementsov
Author by

Isaac Krementsov

I am an enthusiastic web and mobile developer with a versatile skill set. At the beginning of my programming experience, I recognized that I was never done learning. Since then, I have become most familiar with using ExpressJS, MongoDB, Go, Java, HTML5, and more while continuously adding to that list. Whether you simply want to make your website known or need a real-time application, I am here to help.

Updated on June 04, 2022

Comments

  • Isaac Krementsov
    Isaac Krementsov almost 2 years

    Before you mark this as a duplicate, please understand that none of the answers I have looked at pertain enough to my code to fix the issue. I am trying to fix an error in a web app that I am making to upload files to a home server from the internet. I am currently working on the basic user authentication system. When I try to submit the login form on the home page, I am redirected to a page with an error, part of which says "Cannot read property 'username' of undefined". This message also appears in console. What is happening and how can I fix it? The error directs me to line eight of the home controller, "username = req.body.username". App.js

    var LocalStrategy = require('passport-local').Strategy;
    var flash = require("connect-flash");
    var passport = require("passport");
    var session = require("express-session");
    var expressValidator = require("express-validator");
    var mongoose = require("mongoose");
    var path = require('path');
    var express = require('express');
    var cookieParser = require('cookie-parser');
    var app = express();
    var bodyParser = require('body-parser');
    var ejs = require('ejs');
    var routes = require('./server/routes');   
    routes(app);
    app.set('port', process.env.PORT || 3000);
    app.use(bodyParser.urlencoded({extended: false}));
    app.use(bodyParser.json());
    app.use(express.static(path.join(__dirname, 'public')));
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'ejs');
    app.use(cookieParser());
    mongoose.connect("mongodb://localhost:27017/users");
    app.listen(app.get('port'), function(){
      console.log("server started");
    });
    var db = mongoose.connection;
    app.use(session({
      secret: 'yVVma9ga',
      saveUninitialized: true,
      resave: true
    }));
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(expressValidator({
      errorFormatter: function(param, msg, value) {
          var namespace = param.split('.')
          , root    = namespace.shift()
          , formParam = root;
    
        while(namespace.length) {
          formParam += '[' + namespace.shift() + ']';
        }
        return {
          param : formParam,
          msg   : msg,
          value : value
        };
      }
    }));
    app.use(flash());
    app.use(function(req,res,next) {
      res.locals.success_msg = req.flash('success_msg');
      res.locals.error_msg = req.flash('error_msg');
      res.locals.error = req.flash("error");
      next();
    });
    

    /server/routes.js

    var express = require("express");
    var home = require('../controllers/home');
    var client = require('../controllers/client');
    module.exports = function(app) {
        var router = express.Router();
        app.use('/', router);
        router.get('/', home.index);
        router.get('/client', client.home);
        router.post('/', home.login);
    }
    

    controllers/home.js

    module.exports = {
        index: function(req, res){
            res.render("home");
        },
        login: function(req, res){
            var username = req.body.username;
            var password = req.body.password;
            req.checkBody('username', 'Username is required').notEmpty();
            req.checkBody('password', 'Password is required').notEmpty();
            var errors = req.validationErrors();
            if(errors){
                console.log("YES");
            }else{
                console.log("NO");
            };       
        }
    }
    

    /views/home.ejs (partials are just basic html)

    <% include partials/header %>
    <p>Welcome to your personal server!</p>
    <p>Login!</p>
    <form method="post" action = "/">
    <input placeholder="Username" type="text" name="username">
    <input placeholder="Password" type="password" name="password">
    <input type="submit" value="Submit">
    </form>
    <% include partials/footer %>