给定一个 sessionId,我需要获取快速会话对象。之后,我希望能够编辑这个会话对象,并让它自动更新会话存储中的会话。
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
有谁知道这里发生了什么?... :(
==== 编辑 ====
我也尝试调用 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
并得到相同的输出:(
==== 编辑 ====
没关系, save() 有效。sessionId 应该是 sessionID... /facepalm.