2
我節省req.session.user = user; // where user contains single user object
然後console.log(req.session.user.user_id); // this works here
但在socket.js文件,在afterDisconnect功能session.user
不可用。
以下爲afterDisconnect功能不顯示存儲在會話中afterDisconnect變量(),sails.js
afterDisconnect: function(session, socket, cb) { console.log(session); // this is not the one that we used above, it's new. Why? User.findOne({user_id: session.user.user_id}) .exec(function(err, user){ if(err) res.serverError(); user.online = false; user.save(); }); return cb(); }
代碼它顯示了以下錯誤的用戶/插座斷開連接時。
warn: Socket disconnected, but session could not be loaded to pass to configured disconnect handler: `sails.config.sockets.onDisconnect()`. Will pass a fake, empty session as argument to lifecycle callback. Details: Error: Session could not be loaded