我在访问已保存的req.session对象时遇到问题。它似乎正在通过新的api调用访问新的req.session。
这是我的server.js
const logger = require('morgan');
const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
const cookieParser = require('cookie-parser');
const path = require('path');
const apiRoute = require('./routes/api');
const webpack = require('webpack');
const config = require('./webpack.config');
const app = express();
const compiler = webpack(config);
const port = process.env.PORT || 3000
app.use(express.static(path.join(__dirname, 'dist')));
app.use(logger('dev'))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser("sdd", {signed: true}))
app.use(session({
secret: 'superSecretCodez',
resave:true,
saveUninitialized:true,
cookie: { maxAge: 60000 }
}))
app.use('/api', apiRoute)
app.listen(port, () => {
console.log('Server is listening on port', port);
})
我的初始api请求是获取请求,
router.get('/start', createGame, (req, res) => {
res.status(200).json({response: res.game});
})
function createGame(req, res, next){
if(req.originalUrl === '/api/start'){
const game = new BattleShip();
console.log(game)
res.game = game
req.session.game = res.game
req.session.save(function(err){
console.log('session saved');
console.log("Session Before Redirect: ", req.session);
})
}
next();
}
在查看req.session对象时,req.session.game就在req.session中。
session saved
Session Before Redirect: Session {
cookie:
{ path: '/',
_expires: 2016-10-11T03:18:40.520Z,
originalMaxAge: 60000,
httpOnly: true },
game:
BattleShip {
cpuBoard: [ [Object], [Object], [Object], [Object], [Object] ],
playerBoard: [ [Object], [Object], [Object], [Object], [Object] ],
cpuCoordinates: [],
playerCoordinates: [],
playerSelections: [],
cpuSelections: [] } }
BattleShip {
cpuBoard:
[ [ 'A0', 'A1', 'A2', 'A3', 'A4' ],
[ 'B0', 'B1', 'B2', 'B3', 'B4' ],
[ 'C0', 'C1', 'C2', 'C3', 'C4' ],
[ 'D0', 'D1', 'D2', 'D3', 'D4' ],
[ 'E0', 'E1', 'E2', 'E3', 'E4' ] ],
playerBoard:
[ [ 'A0', 'A1', 'A2', 'A3', 'A4' ],
[ 'B0', 'B1', 'B2', 'B3', 'B4' ],
[ 'C0', 'C1', 'C2', 'C3', 'C4' ],
[ 'D0', 'D1', 'D2', 'D3', 'D4' ],
[ 'E0', 'E1', 'E2', 'E3', 'E4' ] ],
cpuCoordinates: [],
playerCoordinates: [],
playerSelections: [],
cpuSelections: [] }
当我正在发布帖子请求并尝试访问同一会话以更新它时,问题就出现了。
router.post('/setShips', setPlayerShips, createCpuCoordinates, (req, res) => {
res.status(200).json({response: res.cpuCoordinates})
// set up coordinates, players div should be updated with divs
})
function setPlayerShips(req, res, next){
console.log(req)
console.log(req.session)
console.log(req.session.game)
if(req.originalUrl=== '/api/setShips'){
res.playerCoordinates = req.body
req.session.game.BattleShip.playerCoordinates = res.playerCoordinates
}
next();
}
它似乎正在访问一个完全不同的cookie,它没有req.session.game。但是,会话存储中的req对象中有会话对象。
sessionStore:
MemoryStore {
domain: null,
_events:
{ disconnect: [Function: ondisconnect],
connect: [Function: onconnect] },
_eventsCount: 2,
_maxListeners: undefined,
sessions:
{ i2R3lJZxEneAheOciHO9QX7_dCjOyFPG: '{"cookie":{"originalMaxAge":60000,"expires":"2016-10-11T03:18:40.487Z","httpOnly":true,"path":"/"},"game":{"cpuBoard":[["A0","A1","A2","A3","A4"],["B0","B1","B2","B3","B4"],["C0","C1","C2","C3","C4"],["D0","D1","D2","D3","D4"],["E0","E1","E2","E3","E4"]],"playerBoard":[["A0","A1","A2","A3","A4"],["B0","B1","B2","B3","B4"],["C0","C1","C2","C3","C4"],["D0","D1","D2","D3","D4"],["E0","E1","E2","E3","E4"]],"cpuCoordinates":[],"playerCoordinates":[],"playerSelections":[],"cpuSelections":[]}}',
'MLxoJNCuxn29GO-cCZyisjUwdLGhHf6l': '{"cookie":{"originalMaxAge":60000,"expires":"2016-10-11T03:18:41.083Z","httpOnly":true,"path":"/"},"game":{"cpuBoard":[["A0","A1","A2","A3","A4"],["B0","B1","B2","B3","B4"],["C0","C1","C2","C3","C4"],["D0","D1","D2","D3","D4"],["E0","E1","E2","E3","E4"]],"playerBoard":[["A0","A1","A2","A3","A4"],["B0","B1","B2","B3","B4"],["C0","C1","C2","C3","C4"],["D0","D1","D2","D3","D4"],["E0","E1","E2","E3","E4"]],"cpuCoordinates":[],"playerCoordinates":[],"playerSelections":[],"cpuSelections":[]}}' },
generate: [Function] },
sessionID: 'aHfVNblu917HOdlRR1kbIqb9JsUFfM-6',
session:
Session {
cookie:
{ path: '/',
_expires: 2016-10-11T03:18:48.163Z,
originalMaxAge: 60000,
httpOnly: true } },
route:
Route {
path: '/setShips',
stack: [ [Object], [Object], [Object] ],
methods: { post: true } } }
Session {
cookie:
{ path: '/',
_expires: 2016-10-11T03:18:48.163Z,
originalMaxAge: 60000,
httpOnly: true } }
undefined
TypeError: Cannot read property 'BattleShip' of undefined
at setPlayerShips (/Users/dfrank/code/projects/battleShip/api/game_controls.js:34:23)
at Layer.handle [as handle_request] (/Users/dfrank/code/projects/battleShip/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/dfrank/code/projects/battleShip/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/Users/dfrank/code/projects/battleShip/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/dfrank/code/projects/battleShip/node_modules/express/lib/router/layer.js:95:5)
at /Users/dfrank/code/projects/battleShip/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/Users/dfrank/code/projects/battleShip/node_modules/express/lib/router/index.js:330:12)
at next (/Users/dfrank/code/projects/battleShip/node_modules/express/lib/router/index.js:271:10)
at Function.handle (/Users/dfrank/code/projects/battleShip/node_modules/express/lib/router/index.js:176:3)
at router (/Users/dfrank/code/projects/battleShip/node_modules/express/lib/router/index.js:46:12)
at Layer.handle [as handle_request] (/Users/dfrank/code/projects/battleShip/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/dfrank/code/projects/battleShip/node_modules/express/lib/router/index.js:312:13)
at /Users/dfrank/code/projects/battleShip/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Users/dfrank/code/projects/battleShip/node_modules/express/lib/router/index.js:330:12)
at next (/Users/dfrank/code/projects/battleShip/node_modules/express/lib/router/index.js:271:10)
at session (/Users/dfrank/code/projects/battleShip/node_modules/express-session/index.js:438:7)
如何访问最初创建的会话对象?如果我能提供更多信息,请告诉我。谢谢!