Monday, 13 March 2017

Unable to overwrite the session variable in express

I am using a ajax call to set a session variable, like:

 $.ajax({
           url: "url", -- a call to node layer
           type: 'GET',
           success: function (response) {


           }

       });

In node layer , i am doing an API call to set the new session value , like:

getxxx:function (req, res){

        var options = {};
        var self=this;
        options.headers = {};
        options.headers['Authorization'] = req.session.Authorization;
        options.headers['loginId'] = req.session.userName;
        var data={};       

        proxyAPI.getData(common.getEndpointURL("XXX", req.session.userName), {}, options, function(err, response, statusCode){
            data.xxx = response;            
            if(data.xxx && data.xxx.count) {
                req.session.count = data.xxx.count;
                req.session.save();

            }
        });   

    },

My App.js looks like this:

app.use(sessionexpress({
        secret: 'secretKey',
        cookie: {
            maxAge: config.timeout,            
            secure: config.sslEnabled
        },
        rolling: true,
        saveUninitialized: true,
        resave: true,
        store: new MemcachedStore({
            hosts: [config.sessionStore.uri],
            secret: config.sessionStore.secret 
        })
    }));

Scenario:
In the call back function, I need to set the updated value from the API layer to the session variable. Purpose is to use the value (req.session.count) in another page.

The problem is : When i try to access the req.session.count variable in another page , the value doesn't show the updated value instead shows the value assigned at app startup.

What Am I doing wrong in this?

Note: If i change the session variable without any API calls , like this :

req.session.count = 100;
req.session.save();

I am able to get the new session value (i.e 100) in other pages !



via user883822

No comments:

Post a Comment