2012-09-05 131 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