0

Hi. When i print the req.session.mySessValue in UI , the value is empty. I think the assigning of req.session.mySessValue = dt.myValue; (express-session) is not proper. could anyone help me on this. Thanks in advance.my express code is

router.get('/', function(req, res, next) {
        if(!req.xx) {
        return res.redirect('/firstView');  
        }
    var options = {
           .......
        };
        var call = http.request(options, function(resp) {
            resp.on('data', function(dt) {            
                var jsondata = JSON.parse(dt);          
                req.session.mySessValue = dt.myValue;
            });
        });
        call.end();
        call.on('error', function(e) {       
           console.log("error" +e.message)
        });

        var v = {
            title: 'sample',
            req : req       
        }
        res.render('myview', v);

    });
Nizam
  • 67
  • 9
  • Are you sure `dt.myValue` has value? and have you tried `call.on('data', ...)` instead of `resp.on('data', ...)` just like you did with `call.on('error`, ...)`? – mkinawy Dec 11 '15 at 15:32
  • And also you can try this: https://github.com/expressjs/session#sessionsave and see if there is an error coming back – mkinawy Dec 11 '15 at 15:37
  • @mkinawy - i am sure that i get the required response and dt.myValue has value. – Nizam Dec 11 '15 at 15:43
  • have you tried logging the value of req.session.mySessValue somewhere (or even posting it in an alert) right after you assign it, to confirm whether or not it is assigning correctly? – Ieuan Stanley Dec 11 '15 at 15:51
  • Are you using Redis or any other third party Session storage? – mkinawy Dec 11 '15 at 16:03
  • Thanks mkinawy for all your reply, sorry i am new to this concept. i am not aware of Redis. I have just installed and set express-session configuration. I am sure there is no issue with that. Actually i have printed the req.session.mySessValue value right after the line req.session.mySessValue = dt.myValue; i am able to see the value in console,but in UI the value is not getting printed. I have set another session variable outside the call , but that value is getting printed in UI – Nizam Dec 11 '15 at 16:19
  • 1
    @mkinawy - really thanks a lot for your time.i think when i have placed the res.render('myview',v) inside the call like var call = http.request(options, function(resp) { resp.on('data', function(dt) { var jsondata = JSON.parse(dt); req.session.mySessValue = dt.myValue; var v = { title: 'sample', req : req } res.render('myview', v); }); }); I am getting the session value in UI. Thanks again mkinawy – Nizam Dec 11 '15 at 16:31

0 Answers0