简体   繁体   中英

NodeJS express-session (why undefined?)

This is my "log in" layer:

app.post('/sign', function(req, res) {
sess=req.session;

var query = 'SELECT * FROM ?? where ??=? AND ??=?';
var table = ["users", "name", req.body.login, "password", md5(req.body.password)];
query = mysql.format(query, table);
connection.query(query, function(err, rows) {
  if(err) {
    console.log(err);
    return;
  } else if(rows.length > 0) {
    console.log("You have been sucessfully logged in.");
    sess.login = req.body.login;
    console.log(sess.login);
  } else {
    console.log("The name or password is incorrect.");
  }
});
res.end();
});

As you see, the session should be created when the user exists in the database (indeed, console.log(sess.login) shows in console proper content of the session so the session was created). The problem is when I change the layer.

app.get('/', function(req, res) {
  sess = req.session;
  if(sess.login) {
    res.render('indexo');
  } else {
    res.render('index');
  }
  console.log(sess.login);
});

Why the sess.login is undefined here? Here is the rest of my code (above the layers)

/*******************************************/
/**/  var express         = require('express'),
/**/      session         = require('express-session'),
/**/      bodyParser      = require('body-parser'),
/**/      ejs             = require('ejs'),
/**/      mysql           = require('mysql'),
/**/      md5             = require('md5');
/*******************************************/

var app = express();
var connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'simpledatabase'
});

connection.connect(function(error) {
  if(error) {
    console.log("There is a problem with connection to the database.");
    return;
  }
    console.log("Connected with a database.");
});

app.use(session({
    secret: 'test session',
    resave: false,
    saveUninitialized: true
}));
app.use(express.static('public'));
app.use('/js', express.static('bower_components/jquery/dist'));
app.use(bodyParser.json());
app.set('view engine', 'ejs');
app.set('views', 'public/views');

var sess;

The sess.login is visible when I create it for example in this way:

app.post('/sign', function(req, res) {
sess=req.session; 
sess.login = req.body.login; /****** HERE ******/
var query = 'SELECT * FROM ?? where ??=? AND ??=?';
var table = ["users", "name", req.body.login, "password", md5(req.body.password)];
query = mysql.format(query, table);
connection.query(query, function(err, rows) {
  if(err) {
    console.log(err);
    return;
  } else if(rows.length > 0) {
    console.log("You have been sucessfully logged in.");
  } else {
    console.log("The name or password is incorrect.");
  }
});
res.end();
});

But you are aware of that I want to create the session only in case when the user exists in the database.

The problem in short: sess.login is visible only in '/login' layer if I create the session in the 'if'. All connection with database are properly set, all works fine with that.

Thank you for your help.

So your problem here is you're calling res.end() on the outside of your callback for your query. This essentially terminates the response process and you don't get a chance to update req.session because the callback gets called a short time (or longer) later after the response process has ended. Try putting res.end in the callback and I think things will work after that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM