我对nodejs和session有一个奇怪的问题。 我已经将问题追溯到session.save函数,
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;
};
导致这种情况的原因是什么?
快速编辑:
如果我需要外部api(box2d)文件,则只会出现此问题。 var Box2D = require('./ box2d.js'); 该文件有效,因为它带有一个工作演示,它也可以使用我的代码,但是在重新启动节点之后...由于某种原因我打破了会话。套接字仍然有效。
该文件在这里(缩短了谷歌文档) box2D 我搜索了可能存在冲突但没有任何可疑情况出现的关键字。 那个文件相当大......这可能是个问题吗?
答案 0 :(得分: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);
}
}
虽然没有在节点环境中测试过这个。