I'm having a weird problem with nodejs and sessions. I have traced the problem down to session.save function,
TypeError: Cannot read property 'sessionStore' of undefined
at Session.save (C:\Program Files\nodejs\node_modules\express\node_modules\connect\lib\middleware\session\session.js:65:11);
//Code in connect session module where this.req gets its value
var Session = module.exports = function Session(req, data) {
Object.defineProperty(this, 'req', { value: req });
Object.defineProperty(this, 'id', { value: req.sessionID });
if ('object' == typeof data) utils.merge(this, data);
console.log("SESSION CREATED", typeof this.req, "VS" , typeof req);
//outputs SESSION CREATED undefined VS object
};
//The session.save function, here the this.req is undefined and it causes the error
Session.prototype.save = function(fn){
this.req.sessionStore.set(this.id, this, fn || function(){});
return this;
};
What could be causing this?
quick edit:
This problem only occurs if i require an external api(box2d) file. var Box2D = require('./box2d.js'); That file works since it came with a working demo, and it worked with my code too, but then after restarting node...for some reason i breaks the sessions. Sockets still work.
The file is here (shortened google docs) box2D I searched it for keywords that might conflict but nothing suspicious shows up. That file is fairly large...could that be a problem ?
It's the custom implementation of defineProperty (in Box2D) that breaks connect. Try this fiddle, then remove the defineProperty function.
You will first be alerted with undefined. After you remove the code, it will alert [objectObject] (as it should).
if(!(Object.prototype.defineProperty instanceof Function)
&& Object.prototype.__defineGetter__ instanceof Function
&& Object.prototype.__defineSetter__ instanceof Function)
{
Object.defineProperty = function(obj, p, cfg) {
if(cfg.get instanceof Function)
obj.__defineGetter__(p, cfg.get);
if(cfg.set instanceof Function)
obj.__defineSetter__(p, cfg.set);
}
}
Have not tested this in the node environment though.