var redis = require('redis');
var router = express.Router();
var mysql = require('mysql');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var client = redis.createClient();
var session = require('express-session'),
RedisStore = require('connect-redis')(session);
var app = express();
app.use(cookieParser);
app.use(session({
store : new RedisStore({
host:'localhost',
port:7351,
maxAge: 300000,
client:client,
ttl:260
}
),
secret:'dafdsa',
resave:false,
saveUninitialized:true,
}));
这是我的设置,这是我的代码
router.post('/', function(req, res, next) {
var id = req.body.userid;
var passwd = req.body.password;
var sess = req.session;
var login_chk = pool.getConnection(function(err,connection){
connection.query('SELECT U_passwd FROM Member WHERE U_id = ?',[id],function(err,row,req){
if(err)console.log(err);
var sqlpasswd = row[0].U_passwd;
var answer = 0;
answer = passwd_chk(passwd,sqlpasswd);
if(answer ===1){
save_session(id);
res.render('main');
}
var save_session = function(req){
req.session.id = req;
}
我将发布(登录)并在会话中保存id但这段代码总是错误的,无法设置属性...我该如何解决?... 谢谢你的回答
答案 0 :(得分:1)
这会导致您将userId
作为save_session(req)
参数传递。然后,您在案件中req.session.id = req;
与userId.session.id = userId;
相同。该错误会通知您userId
没有session
属性(猥亵)。您所需要的只是传递req
和userId
作为参数。像这样更改你的代码。
router.post('/', function(req, res, next) {
var id = req.body.userid;
var passwd = req.body.password;
var sess = req.session;
var login_chk = pool.getConnection(function(err,connection){
connection.query('SELECT U_passwd FROM Member WHERE U_id = ?',[id],function(err,row,req){
if(err)console.log(err);
var sqlpasswd = row[0].U_passwd;
var answer = 0;
answer = passwd_chk(passwd,sqlpasswd);
if(answer ===1){
save_session(req,id); <--- Change this
res.render('main');
}
var save_session = function(req,id){ <--- Change this
req.session.id = id; <--- Change this
}
我希望它有所帮助。