1

main.js (only the relevant parts)

var express = require('express');
var app = require('express')(),
    expressSession = require('express-session'),
    cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

// Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })

// Access Middleware
var connect = require('connect');
var http = require('http');
var session = require('express-session');

app.use(express.static('public'));
app.use(expressSession({
    secret: 'SECKEY',
    resave: true,
    saveUninitialized: true
}));

app.post('/process_post', urlencodedParser, function (req, res) {
    // Prepare output in JSON format
    var response = {
        first_name:req.body.first_name,
        last_name:req.body.last_name
    };
    console.log(response);


    var post = req.body;
    if(post.first_name === 'u' && post.last_name === 'p') {
        res.session.user_id = 3;
        res.redirect('/home');
    } else {
        res.send('INVALID');
    }

    res.end(JSON.stringify(response));
});

This is the error I get:

TypeError: Cannot set property 'user_id' of undefined
   at D:\Users\Winston\Code\NodeJS Projects\Sample\main.js:43:29
   at Layer.handle [as handle_request] (D:\Users\Winston\Code\NodeJS Projects\Sample\node_modules\express\lib\router\layer.js:95:5)
   at next (D:\Users\Winston\Code\NodeJS Projects\Sample\node_modules\express\lib\router\route.js:131:13)
   at D:\Users\Winston\Code\NodeJS Projects\Sample\node_modules\body-parser\lib\read.js:121:5
   at invokeCallback (D:\Users\Winston\Code\NodeJS Projects\Sample\node_modules\body-parser\node_modules\raw-body\index.js:262:16)
   at done (D:\Users\Winston\Code\NodeJS Projects\Sample\node_modules\body-parser\node_modules\raw-body\index.js:251:7)
   at IncomingMessage.onEnd (D:\Users\Winston\Code\NodeJS Projects\Sample\node_modules\body-parser\node_modules\raw-body\index.js:308:7)
   at IncomingMessage.emit (events.js:104:17)
   at _stream_readable.js:908:16
   at process._tickCallback (node.js:355:11)

req.session isn't working, but I think I defined everything. I have installed connect and express-session middleware. How can I debug problems like this in the future?

EDIT 1: Print out req.session

{ cookie:
   { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true } }
{ first_name: 'u', last_name: 'p' }
{ cookie:
   { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true } }
2
  • check this SO thread stackoverflow.com/questions/21093787/… Commented Mar 3, 2016 at 3:58
  • I have implemented that solution. Error: Most middleware (like session) is no longer bundled with Express and must be installed separately. Pl ease see https://github.com/senchalabs/connect#middleware. It cries at express.session. I believe I have already installed express-session and put it in package.json? Could this be the issue? Commented Mar 3, 2016 at 4:39

2 Answers 2

3

You're calling res.session not req.session

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for responding, I fixed it but the exact same error persists.
1

You change your code below:

res.session.user_id = 3;

to

req.session.user_id = 3;

5 Comments

Thanks for responding, I fixed it but the exact same error persists
Can you show req.session result and your error for me?
you can check "req.session.identify" variable information? If it exist, you can see user_id of you in this object.
It would appear that req.session.identify is undefined, but there is more info
I read about "EDIT 1: Print out req.session" of you. req.session information result is fully?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.