0

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 ?

aynber
  • 22,380
  • 8
  • 50
  • 63
Rainer Plumer
  • 3,693
  • 2
  • 24
  • 42
  • Is it possible that for some reason your `utils.merge()` function is killing the `req` property you are assigning? – sp0rkyd0rky Nov 12 '13 at 21:53
  • Ill check out what the data contains, but this code is 100% from node core so its not really my code...other than the console.log ofc data logged out as "undefined", so that line shouldnt run at all – Rainer Plumer Nov 12 '13 at 22:01
  • the `utils.merge()` is from the node core? i don't see it in the docs: http://nodejs.org/api/util.html – sp0rkyd0rky Nov 13 '13 at 16:27
  • both session and utils are from Connect framework based on node...and is widely used. So it cant be that. I updated my post with more info, which might help – Rainer Plumer Nov 13 '13 at 16:59
  • I made an update to node, from v0.8 to 10 and now this error is even more bizarre. now a line like this console.log("Some text"); triggers an error "Cannot call method 'write' of undefined" And again this only happens if i include the file linked in my original post. How can it conflict ? – Rainer Plumer Nov 13 '13 at 22:45

1 Answers1

1

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);
      }
   }

http://jsfiddle.net/nqUrQ/

Have not tested this in the node environment though.

Johan
  • 101
  • 4