简体   繁体   English

NodeJS表达会话(为什么未定义?)

[英]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). 如您所见,应该在用户存在于数据库中时创建会话(实际上,console.log(sess.login)在控制台中显示了会话的正确内容,因此创建了会话)。 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? 为什么在这里未定义sess.login? 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: 例如,以这种方式创建时,sess.login是可见的:

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'. 简而言之,问题是:如果我在'if'中创建会话,则sess.login仅在'/ login'层中可见。 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. 因此,这里的问题是您要在查询的回调外部调用res.end() 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. 这实际上终止了响应过程,并且您没有机会更新req.session,因为在响应过程结束之后的一小段时间(或更长时间)内将调用回调。 Try putting res.end in the callback and I think things will work after that. 尝试将res.end放入回调中,我认为此后一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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