gpt4 book ai didi

javascript - 快速 session 无法设置未定义的属性 'id'

转载 作者:IT王子 更新时间:2023-10-29 06:14:14 26 4
gpt4 key购买 nike

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;
}

我将发布(登录)并在 session 中保存 ID,但此代码总是错误,无法设置属性...我该如何解决?...谢谢解答

最佳答案

发生这种情况是因为您将 userId 作为 save_session(req) 参数传递。然后你正在做 req.session.id = req; 在你的情况下与 userId.session.id = userId; 相同。该错误通知您 userId 没有 session 属性(显然)。您只需要将 requserId 作为参数传递即可。像这样更改您的代码。

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
}

希望对你有帮助。

关于javascript - 快速 session 无法设置未定义的属性 'id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36882562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com