2012-09-05 71 views
0

给定一个sessionId,我需要保留快速会话对象。之后,我希望能够编辑此会话对象,并让它自动更新会话存储中的会话。重新创建Express会话?

Session = require('express').session.Session; 

_testSession: (sStore, sId) -> 
    console.log 'called _testSession with session id: ' + sId 

    sStore.get sId, (error, session) -> 
    expressSession = new Session 
     sessionId: sId 
     sessionStore: sStore 
     , session 
    expressSession.foo = 17 

    console.log 'set expressSession.foo to: ' + expressSession.foo 

    setTimeout (()-> 
     sStore.get sId, (error, session) -> 
      console.log 'foo = ' + session.foo 
     ), 1000 

但是,运行这个测试时,我得到以下的输出:

called _testSession with session id: YaFLcq3eSvoNuwnLS1T1ntrC.3UJMuQ5So6lYRWLDgIRx4Vk/Ab1qH65zV9IwMqoMcR8 
set expressSession.foo to: 17 
foo = undefined 

有没有人有任何想法是怎么回事... :(

=== = EDIT ====

我试图调用save()为好,以便代码读取:

console.log 'set expressSession.foo to: ' + expressSession.foo 

expressSession.save (error) -> 
    console.log 'error: ' + error 
    setTimeout (()-> 
     sStore.get sId, (error, session) -> 
      console.log 'foo = ' + session.foo 
     ), 1000 

,并得到了相同的输出:(

==== ====编辑

没关系,保存()的作品。 sessionId应该是sessionID .../facepalm。

+0

什么你的意思是“自动更新”?你确定你不需要调用Session对象的save方法吗?http://www.senchalabs.org/connect/session.html – robkuz

+0

我更新了我的帖子。可悲的是,调用save()没有帮助:( – user1161657

+0

Nvm,sessionId应该是sessionID。 – user1161657

回答

1

正如@robertj所说,除非您告诉它保存,否则您可能看不到保存的对象。

但更重要的是,除非您专门打算测试会话对象本身(即连接会话的中间件),否则没有意义您正在做什么。

没有更多的上下文很难说,但我期望你的目的是确保某些路由处理程序或中间件将某些东西放入会话中,是的?如果是这样,只需单独进行测试。例如

var should = require('should'); 

var req = { session : {}, /*any other bits of req you expect your handler to need */}; 
var res = { /* whatever functions your handler needs off the response */ }; 

yourHandler(req, res); 
req.session.foo.should.be.ok; // not null; alternatively test equality or whatever 

这是'单元测试'。如果你正在做一个集成测试(你想看到事情一起工作,如db和view等),那么你仍然不需要检查会话,你应该检查它的影响是

+0

其实,我的目标是在使用socket.io时使用会话对象。 – user1161657

+0

可能应该问这个问题。已经有这样的例子:http://www.danielbaulig.de/socket-ioexpress/ – Paul

+0

这实际上是我提到的检索套接字信息的东西,但我仍然坚持这一部分,但可悲的是。 – user1161657