node.js - Session variables are undefined out of the scope it is set to a value in nodejs -


i using nodejs , trying access session variable in route not same route session variable defined , set. keeps telling me session variable not defined!

this set session variable:

var http      = require('http'); var express    = require('express'); var bodyparser = require('body-parser'); var mysql      = require('mysql'); var urlencodedparser =  bodyparser.urlencoded({ extended: false }) var app = express(); var dateformat = require('dateformat'); var nodemailer = require('nodemailer'); var cookieparser = require('cookie-parser') var session = require('express-session');   app.use(cookieparser());  app.use(session({     secret: "this_is_a_secret",     resave: false, }));       app.post('/auth',urlencodedparser,function (req, res) {          conn.query('select * users user_name="'+req.body.username+'" , user_pass="'+req.body.password+'"',function(err,res1){             if(err) {                 console.log("error!");                 res.send('-2');             }             else{                 if(res1.length==0) {                     console.log("no matching!");                     res.send('-2');                 }                 else if(res1.length==1){                     req.session.username = req.body.username; //this session variable                     console.log("successful login with: " + req.session.username); //this works fine                     res.send('1');                 }             }         });     }); 

this second route trying access req.session.username:

app.post('/privilage',urlencodedparser,function (req, res) {  console.log("privilage: " + req.session.username);  //here need check , says "undefined"      if(!req.session.username){         res.send('-2');     }     else res.send('1'); }); 

what wrong exactly??

thanks :)

you don't use store session. it's means sessions store in memory , reset on application restart.

app.use(session({     secret: "this_is_a_secret",     resave: false,     store: ... })); 

be sure don't restart application between login , check requests.


also recommend make changes in code

conn.query('select * users user_name="'+req.body.username+'" , user_pass="'+req.body.password+'"', function(err, qres){     if(err) {         console.log("error!", err.message);         return res.send('-2'); // maybe best way res.status(401).end()?     }      if (qres.length == 0) {         console.log("no matching!");         return res.send('-2');     }      if (qres.length != 1) // unbelievable case, check         throw new error("smth wrong");      req.session.username = req.body.username;      console.log("successful login with: " + req.session.username);                          res.send('1'); } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -