ExpressJS Session Error: req.session.touch() is not a function

If you are using the NodeJS framework ExpressJS and are trying to save something into the session, maybe you get this error. Don’t worry, very easy to fix.

req.session.touch(); TypeError: undefined is not a function

How to reproduce the error

Try to replace the session object

// Error ahead! 
req.session = { 'whatever': 'crash' }; 
req.session = 1; // This too

Error cause

You can’t replace the session object, it has own functions and properties.

So ExpressJS launches this crash if detects you are modifying it.

How to fix the session.touch error

Just add your data as session’s properties

// This works 
req.session.user = { 'id': 123 }; 
req.session.pageviews = 1; // This too

Leave a comment for any suggestion, comment or doubt!

David Burgos

Read more posts by this author.