Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6938841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:32:37+00:00 2026-05-27T12:32:37+00:00

In a current project (kind of a shop system), I use node.js with expressJS

  • 0

In a current project (kind of a shop system), I use node.js with expressJS and connect-mongo as session store. On client side, I use a single request at startup to create a new session and send multiple parallel requests to the node.js server afterwards.
Because these parallel requests change the session, those changes seem to overwrite each other, although they change different objects of the session, of course.

Example (all 3 requests start at the same time):

  • Request A pushes some products to the array req.session.productHist['abc']
  • Request B pushes products to req.session.productHist['def']
  • Request C takes some time, but doesn’t change the session

Because request C finishes after request A and B, but starts before they finished, it seems to overwrite session.productHist with the value it held when request C started (null).

How can I fix this?

Update:

Some example code with console output:

var url = require('url'),
    express = require('express'),
    MongoStore = require('connect-mongo');

var aDay = 24*60*60*1000;

var app = express.createServer();

app.configure(function(){
  app.use(express.cookieParser());
  app.use(express.session({
    secret: "secret",
    store: new MongoStore({ db: 'lmsCache' }),
    maxAge: aDay
    })
  );
  app.use(express.methodOverride());    app.use(express.bodyParser());
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
  app.use(app.router);
  app.use(express.logger());
});


function sendStringified(req, res, data) {
    data = JSON.stringify(data);
    if (req.url_query.callback) { data = req.url_query.callback + "(" + data + ");"; }
    res.send(data);
}

function parseParams(req,res,next) {
  req.url_query = url.parse(req.url,true).query;
  next();
}

function doExpensiveStuff(req,res,next) {
  console.log("######################### init start");
  [...]
}


app.get('/init', parseParams, doExpensiveStuff, function(req,res) {
  console.log("init: session.productHist: " + JSON.stringify(req.session.productHist));
  console.log("######################### init end");
  sendStringified(req,res,null);
});


app.get('/products', parseParams, function(req,res) {

  console.log("######################### products "+req.url_query.category+" start");

  if(!req.session.productHist[req.url_query.category])
    req.session.productHist[req.url_query.category] = [];

  for(var i=0;i<2;i++) {
      req.session.productHist[req.url_query.category].push({ "id": new Date().toGMTString() }); 
  } 

  console.log("products: session.productHist: " + JSON.stringify(req.session.productHist));
  console.log("######################### products "+req.url_query.category+" end");
  sendStringified(req,res,[]);
});

app.get('/newSession', parseParams, function(req,res) {
  console.log("######################### newSession");
  req.session.productHist = {};
  sendStringified(req,res,true);
});  

app.listen(8080);  

time = new Date().toGMTString();  

console.log('Server starting at: ' + time);  

Console log:

Server starting at: Thu, 15 Dec 2011 15:50:37 GMT

################### newSession

################### init start

################### products -1 start

products: session.productHist: {“-1”:[{“id”:”Thu, 15 Dec 2011 15:50:40 GMT”},{“id”:”Thu, 15 Dec 2011 15:50:40 GMT”}]}

################### products -1 end

init: session.productHist: {}

################### init end

[…]

################### products -1 start

products: session.productHist: {“-1”:[{“id”:”Thu, 15 Dec 2011 15:50:53 GMT”},{“id”:”Thu, 15 Dec 2011 15:50:53 GMT”}]}

################### products -1 end

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:32:37+00:00Added an answer on May 27, 2026 at 12:32 pm

    I think I have found the answer to this tricky problem.

    From the Express.js documentation:

    Properties on req.session are automatically saved on a response

    Short version

    When you set a session variable (req.session.my_var = value), it’s not actually saved then (at that exact moment), but later (when you send the response, in your case it’s when you do res.send). That caused your problem.

    Long version

    So what does that mean exactly?

    1. You make a request, then get the session variable (which is in state A) and after that do something with it (that requires some time)
    2. You make another request and get the session variable (which is still in state A because the response hasn’t been sent yet) and change some stuff to it
    3. Now the session processing etc has been done so you send the response from 1), thus modifying the session variable and bringing it into state B
    4. Here comes the ‘fun’ part, after you sent the response from 2). Now you aren’t actually modifying the current session (which has been updated into state B), because the response was delayed, so what you’re actually changing is the session from state A, bringing it into state C => which means all the modifications from state B were lost!
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My current project is broken down into 3 parts: Website, Desktop Client, and a
In my current project, my client application first talk with the server and the
I'm planning to use the Multiple apk approach to my current project, since it
In my current project, we got a rather unusual request(to me). The client wants
in my current project i need to realize questionnaire (survey) constructor functionality (kind of
My current project is in Rails. Coming from a Symfony (PHP) and Django (Python)
My current project uses NUnit for unit tests and to drive UATs written with
My current project involves deploying an upgraded .exe file that runs as a Windows
My current project involves writing Perl code inside a Solaris VMWare appliance (hosted on
Our current project has ran into a circular dependency issue. Our business logic assembly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.