1

My problem is that req.body becomes undefined when I try to render a page.

My app code:

var https = require('https');
var fs = require('fs');
var mongourl = "mongodb://localhost:27017/auntyinda";
var mongoose = require('mongoose');
fs.readdirSync(__dirname + '/models').forEach(function (filename) { 
  if (~filename.indexOf('.js')) require(__dirname + "/models/" + filename);
});
//var queries = require('./mongoq/query')

var express = require('express');
var path = require('path');
//var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var expressSession = require('express-session');
var passport = require('passport');

var myPass = require('./security/auth')
var routes = require('./routes/index');
//var users = require('./routes/users');

var app = express();
var server = https.createServer({
  cert: fs.readFileSync(__dirname + '/my.crt'),
  key: fs.readFileSync(__dirname + '/my.key')
},app).listen(4000);

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
//app.configure(function(){

app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//app.use(require('connect').bodyParser())
app.use(cookieParser());
app.use(expressSession( {
  secret: process.env.SESSION_SECRET || 'sonic12',
  resave: false,
  saveUninitialized: false
  }))
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));

//});

mongoose.connect(mongourl)
//app.use('/', myPass);
app.use('/', routes);
//app.use('/users', users);


//app.locals.appdata = require("./data.json")

My route code:

router.post('/register', function(req, res, next) {
  regcheck(req.body.username, req.body.password, req.body.passwordConfirm, function (error) {
    if (error) {
      console.log(req.body) //OUTPUT IS WHAT IS EXPECTED. NICE JSON FORMAT.
      res.render('register', {
        title: 'Register',
        classname: 'register',
        socialIcons: socialIcons,
        isAuthenticated: false,
        regdata: req.body,
        err: error
      })
    }
  })
})

I want the render to use the regdata object to refill data that was good back into the registration form. But for some strange reason it becomes undefined by the time it reaches the render.

My EJS file contains this line at the top:

<% console.log('Error recieved: '); console.log(err); console.log(regdata); %>

But the output of the console.log(regdata) is undefined. What is happening here? The err prints as it should. Thanks in Advance for your help.

  • try something like `regdata: _.clone(req.body)` or any cloning function or anything from http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-an-object – Ashok Kumar Sahoo Nov 05 '15 at 12:26
  • I used `npm install clone` as one of the recommended options in your link, required it and used it but still got undefined by the time ejs gets access to it. – seamusgalla Nov 05 '15 at 12:51

1 Answers1

0

Try this, <pre><%= regdata %></pre>

I think you are missing out a = somewhere. Consult the docs.

Ashok Kumar Sahoo
  • 580
  • 3
  • 8
  • 24
  • It doesn't seem to be this. The problem seems to be with ejs. I have narrowed to problem down further in this question http://stackoverflow.com/questions/33547629/ejs-include-wont-pass-local-variable – seamusgalla Nov 05 '15 at 14:55