Can't get sessions to work - TypeError: Cannot set property 'uid' of undefined

4,323 views
Skip to first unread message

Anthony Crognale

unread,
Mar 5, 2011, 3:22:53 PM3/5/11
to Express
Hello all,

I've been having trouble with getting sessions to work on express
2.0.0beta with connect 1.0.3. I've tried using connect-redis, connect-
mongodb, and even memorystore to no avail. Here's the issue:

I'll try and set a session property like "req.session.uid = user._id",
but then node returns (anywhere I try it):

TypeError: Cannot set property 'uid' of undefined
at Object.<anonymous> (/home/ubuntu/impromptu/app.js:257:22)
at param (/usr/local/lib/node/.npm/connect/1.0.3/package/lib/
middleware/router.js:147:21)
at pass (/usr/local/lib/node/.npm/connect/1.0.3/package/lib/
middleware/router.js:163:10)
at /usr/local/lib/node/.npm/connect/1.0.3/package/lib/middleware/
router.js:153:19
at Object.<anonymous> (/usr/local/lib/node/.npm/express-form/0.5.1/
package/lib/form.js:114:15)
at param (/usr/local/lib/node/.npm/connect/1.0.3/package/lib/
middleware/router.js:147:21)
at pass (/usr/local/lib/node/.npm/connect/1.0.3/package/lib/
middleware/router.js:163:10)
at Object.router [as handle] (/usr/local/lib/node/.npm/connect/
1.0.3/package/lib/middleware/router.js:169:6)
at next (/usr/local/lib/node/.npm/connect/1.0.3/package/lib/
http.js:204:15)
at Object.methodOverride [as handle] (/usr/local/lib/node/.npm/
connect/1.0.3/package/lib/middleware/methodOverride.js:35:5)

I've tried reinstalling node, express, connect, etc and even putting
it on a different server with the same versions - still no luck.

Using node 0.4.2, connect 1.0.3, express 1.0.7. Date and time are set
correctly on the server for what it's worth...

Any ideas? Can provide more info if needed. Here's my app.configure:

app.configure( function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(express.cookieParser());
app.use(express.session({store: memStore({reapInterval: 60000 *
10}) , secret: 'is a secret'}));
});
app.configure('development', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStack:
true }));
});

(var memStore = require('connect').session.MemoryStore;)

James Charlesworth

unread,
Mar 5, 2011, 8:06:04 PM3/5/11
to expre...@googlegroups.com, Anthony Crognale
I had the exact same problem.  I was unable to get connect-mongodb to work with connect 1.0 and the developer of connect-mongodb is not longer supporting it so im not sure if that is ever gonna get fixed (or even if it needs to, we may be both doing something else wrong). 

However, I was able to get sessions working with express, just not into mongo or redis. I beleive it depends on the order of setting the session call in your config.  You can see my configuration below, which works in express 2.0-beta, node 0.50-pre and Connect 1.0.3:

app.use(express.static(__dirname + '/public'));
app.use(express.favicon());
app.use(express.bodyParser());
app.use(express.cookieParser());

app.use(express.methodOverride());
        app.use(express.session({ secret: 'topsecret' }));
app.use(app.router);
      app.use(stylus.middleware({
        src: __dirname + '/public'
      , dest: __dirname + '/public'
      , compile: compile 
    }));

I think it breaks with app.router being called before express.session. Hope that helps.

James



--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.


Laurie Harper

unread,
Mar 5, 2011, 8:16:38 PM3/5/11
to expre...@googlegroups.com
On 2011-03-05, at 8:06 PM, James Charlesworth wrote:
> I think it breaks with app.router being called before express.session. Hope that helps.


That would do it, yes; if app.router is installed before express.session, it means the session middleware wont be run until after your routes have fired. Therefore, req.session isn't set up until afterwards, either.
--
Laurie Harper
http://laurie.holoweb.net/

Anthony Crognale

unread,
Mar 5, 2011, 8:59:55 PM3/5/11
to expre...@googlegroups.com
Thanks so much, that fixed it right up. Glad it was as simple as that, now
to figure out what to do about my session store.

-Anthony


-----Original Message-----
From: expre...@googlegroups.com [mailto:expre...@googlegroups.com] On
Behalf Of Laurie Harper
Sent: Saturday, March 05, 2011 8:17 PM
To: expre...@googlegroups.com
Subject: Re: [Express-js] Can't get sessions to work - TypeError: Cannot set
property 'uid' of undefined

--

Casey Banner

unread,
Mar 5, 2011, 11:20:57 PM3/5/11
to expre...@googlegroups.com
If you find a solution for mongodb session stores, pleas let us know.
I too am looking for a connect-mongodb replacement.

vision media [ Tj Holowaychuk ]

unread,
Mar 6, 2011, 2:01:23 PM3/6/11
to expre...@googlegroups.com
you cannot use express < 2.0 with connect 1.x
Tj Holowaychuk
Vision Media
President & Creative Lead

Rekha Shankar

unread,
Oct 21, 2015, 1:05:26 AM10/21/15
to Express, ant...@bitsolar.com, james.cha...@gmail.com
Thanks James. Your solution saved a lot of my time.
Reply all
Reply to author
Forward
0 new messages