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 ?

4

1 回答 1

1

中断连接的是 defineProperty(在 Box2D 中)的自定义实现。试试这个小提琴,然后删除 defineProperty 函数。

您将首先收到未定义的警报。删除代码后,它会提醒 [objectObject](应该如此)。

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/

虽然还没有在节点环境中测试过。

于 2013-11-15T10:58:18.247 回答