无法在NodeJ中保存快速会话

无法在NodeJ中保存快速会话

本文介绍了无法在NodeJ中保存快速会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用户的userName保存在快速会话中。它将变量保存在会话中,但是在路由的下一个请求中返回 undefined 。但是,问题仅在于POST路由。



使用GET请求,我可以保存会话变量,并且在进一步的请求时不会被销毁。我想我一定在做错事。我尝试使用 save()方法,但仍然只保存第一次,下次再次消失。



以下是代码:

  app.use(session({
secret:'some secret',
resave:true,
saveUninitialized:true,
cookie:{
maxAge:36000000,
httpOnly:false
},
}));

以下获取路由工作。

  app.get(/ test,function(req,res,next){
if(!req.session.name){
req.session.name = vikas kumar;
} else {
console.log(req.session.name);
}
res.end();
});

app.use(function(req,res,next){
var userName = req.session.userName;
// console.log(req.session.userName) ;
if(userName&& userName!='undefined'){
res.send({status:success,value:userName});
} else {
if(req.path =='/ checkIfLoggedIn'){
res.send({status:error,message:Session ending。});
} else {
next('route');
}
}
});

只有一次路由保存会话。

  app.post('/ getProfile',function(req,res,next){
console.log(Session,req.session.userName,req.session。 name);
if(typeof req.session.userName!='undefined'){
var userName = req.session.userName;
} else {
var userName = req .body.userName;
}
connectionPool.getConnection(function(err,connection){
if(err){
res.send({status:error,message :err});
} else {
connection.query(SELECT * FROM cs_chat.users WHERE username =?,[userName],function(err,rows,fields){
if(rows.length == 1){
// console.log(>>>,req.session.userName);
req.session.userName = userName;
req.session.save();
// console.log(<<,req.session.userName);
下一个();
} else {
res.send({status:error,message:对不起,你没有注册。
}
connection.release();

});

}
});
},function(req,res){
connectionPool.getConnection(function(err,connection){
if(err){
res.send({status:error ,message:err});
} else {
req.session.username = req.body.userName;
req.session.save();
connection.query (SELECT usr.username,usr.name,tsn.sender,CASE WHEN tsn.unseen is not null THEN tsn.unseen ELSE 0 END as unseen,tsn.receiver FROM users usr LEFT JOIN(SELECT sender,receiver,SUM(CASE看到= 0 THEN 1 ELSE 0 END)AS看不见的消息WHERE receiver =?GROUP BY sender,receiver)tsn ON usr.username = tsn.sender where usr.username!=?,[req.body.userName,req .body.userName],function(err,rows,fields){
if(rows.length> 0){
res.send({status:success,values:rows});
} else {
res.send({status:success,values:[]});
}
connection.release();
});

}
});
});

我做错了吗?

Javascript 你应该使用 === 检查质量的 == ,并且为了更好的响应时间释放连接,然后再次连接到它,这是简单的不必要的,当你连接到 connectionPool 只需查询两次,然后释放连接。



这是一个示例修改的 app.post().. 请求,这应该工作:

  app.post('/ getProfile',function(req,res,next){
console.log(Session req.session.userName,req.session.name);
var userName =(req.session.userName!== undefined)?req.session.userName:req.body.userName;

connectionPool.getConnection(function(err,connection){
if(err){
res.send({status:error,message:err});
} else {
connection.query(SELECT * FROM cs_chat.users WHERE username =?,[userName],function(err,rows,fields){
if(rows.length === 1){
req.session.userName = userName;
req.session.save();
} else {
res.send({status:error,message:对不起,你没有注册。});
}
});
connection.query(SELECT usr.username,usr.name,tsn.sender,CASE WHEN tsn.unseen is not null THEN tsn.unseen ELSE 0 END as unseen,tsn.receiver FROM users usr LEFT JOIN(SELECT sender,receiver, SUM(CASE WHEN见= 0 THEN 1 ELSE 0 END)AS看不见的消息WHERE receiver =? GROUP BY sender,receiver)tsn ON usr.username = tsn.sender where usr.username!=?,
[req.body.userName,req.body.userName],
function(err,行,字段){
if(rows.length> 0){
res.send({status:success,values:rows});
} else {
res.send({status:success,values:[]});
}
});
connection.release();
}
});
});


I'm trying to save the userName of a user in express session. It saves the variable in session but it returns undefined on next request to the route. However, the problem is with POST route only.

With GET request, I can save session variable and it doesn't get destroyed on further requests. I think I must be doing something wrong. I tried save() method for POST but still it saves only first time and next time it is vanished.

Here's the code:

app.use(session({
  secret: 'some secret',
  resave: true,
  saveUninitialized: true,
  cookie: {
    maxAge: 36000000,
    httpOnly: false
  },
}));

FOLLOWING GET ROUTE WORKS.

app.get("/test", function(req, res, next){
    if(!req.session.name){
        req.session.name = "vikas kumar";
    } else {
        console.log(req.session.name);
    }
    res.end();
});

app.use(function (req, res, next) {
  var userName = req.session.userName;
  // console.log(req.session.userName);
  if(userName && userName != 'undefined'){
    res.send({status: "success", value: userName});
  } else {
    if(req.path == '/checkIfLoggedIn'){
        res.send({status: "error", message: "Session ended."});
    } else {
        next('route');
    }
  }
});

FOLLOWING ROUTE SAVES SESSION ONLY ONCE.

app.post('/getProfile', function (req, res, next) {
    console.log("Session", req.session.userName, req.session.name);
    if(typeof req.session.userName != 'undefined'){
        var userName = req.session.userName;
    } else {
        var userName = req.body.userName;
    }
   connectionPool.getConnection(function (err, connection) {
        if (err) {
            res.send({status: "error", message: err});
        } else {
        connection.query("SELECT * FROM cs_chat.users WHERE username=?", [userName], function (err, rows, fields) {
            if(rows.length==1){
                // console.log(">>>", req.session.userName);
                req.session.userName = userName;
                req.session.save();
                // console.log("<<<", req.session.userName);
                next();
            }else{
                res.send({status: "error", message: "Sorry, you're not registered."});
            }
            connection.release();

        });

        }
    });
}, function (req, res) {
    connectionPool.getConnection(function (err, connection) {
        if (err) {
            res.send({status: "error", message: err});
        } else {
            req.session.username = req.body.userName;
            req.session.save();
        connection.query("SELECT usr.username, usr.name, tsn.sender, CASE WHEN tsn.unseen is not null THEN tsn.unseen ELSE 0 END as unseen, tsn.receiver FROM users usr LEFT JOIN (SELECT sender, receiver, SUM(CASE WHEN seen = 0 THEN 1 ELSE 0 END) AS unseen FROM messages WHERE receiver = ? GROUP BY sender , receiver) tsn ON usr.username = tsn.sender where usr.username!=?", [req.body.userName, req.body.userName], function (err, rows, fields) {
            if(rows.length>0){
                res.send({status: "success", values: rows});
            }else{
                res.send({status: "success", values: []});
            }
            connection.release();
        });

        }
    });
});

Am I doing something wrong?

解决方案

Since it is Javascript you should be checking quality with === instead of == and for better response time releasing the connection and then again connecting to it, is simply unnecessary, when you have the connection to connectionPool just query from it twice and then release the connection.

Here is the sample modified app.post().. request, this should work:

    app.post('/getProfile', function (req, res, next) {
    console.log("Session", req.session.userName, req.session.name);
    var userName = (req.session.userName !== undefined) ? req.session.userName : req.body.userName;

    connectionPool.getConnection(function (err, connection) {
        if (err) {
            res.send({ status: "error", message: err });
        } else {
            connection.query("SELECT * FROM cs_chat.users WHERE username=?", [userName], function (err, rows, fields) {
                if (rows.length === 1) {
                    req.session.userName = userName;
                    req.session.save();
                } else {
                    res.send({ status: "error", message: "Sorry, you're not registered." });
                }
            });
            connection.query("SELECT usr.username, usr.name, tsn.sender, CASE WHEN tsn.unseen is not null THEN tsn.unseen ELSE 0 END as unseen, tsn.receiver FROM users usr LEFT JOIN (SELECT sender, receiver, SUM(CASE WHEN seen = 0 THEN 1 ELSE 0 END) AS unseen FROM messages WHERE receiver = ? GROUP BY sender , receiver) tsn ON usr.username = tsn.sender where usr.username!=?",
                [req.body.userName, req.body.userName],
                function (err, rows, fields) {
                    if (rows.length > 0) {
                        res.send({ status: "success", values: rows });
                    } else {
                        res.send({ status: "success", values: [] });
                    }
                });
            connection.release();
        }
    });
});

这篇关于无法在NodeJ中保存快速会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 03:07