simple social network using node.js and mongodb -
i trying build simple social network , following book(building node applications mongodb , backbone)(https://github.com/swiftam/book-node-mongodb-backbone/tree/master/ch10). however, realized node.js version has been updated.
i tied solve issue got problem in chat.js states error:
ch10/routes/chat.js:27 data.sessionstore.load(data.sessionid, function(err, session) { typeerror: cannot read property 'load' of undefined
module.exports = function(app, models) { var io = require('socket.io'); var utils = require('connect').utils; var cookie = require('cookie'); this.io = io; //var session = require('connect').middleware.session.session; var sio = io.listen(app.server); sio.configure(function() { // utility methods see if account online app.isaccountonline = function(accountid) { var clients = sio.sockets.clients(accountid); return (clients.length > 0); }; sio.set('authorization', function(data, accept) { var signedcookies = cookie.parse(data.headers.cookie); // var cookies = utils.parsesignedcookies(signedcookies, app.sessionsecret); // data.sessionid = cookies['express.sid']; data.sessionstore = app.sessionstore; data.sessionstore.load(data.sessionid, function(err, session) { if (err || !session) { accept("error", false); } else { data.session = session; accept(null, true); } }); }); sio.sockets.on('connection', function(socket) { var session = socket.handshake.session; var accountid = session.accountid; var saccount = null; socket.join(accountid); io.use(function (socket, next) { next(); }); // trigger login event // of account app.triggerevent('event:' + accountid, { from: accountid, action: 'login' }); var handlecontactevent = function(eventmessage) { socket.emit('contactevent', eventmessage); }; var subscribetoaccount = function(accountid) { var eventname = 'event:' + accountid; app.addeventlistener(eventname, handlecontactevent); console.log('subscribing ' + eventname); }; // find account contacts , subscribe models.account.findbyid(accountid, function subscribetofriendfeed(account) { var subscribedaccounts = {}; saccount = account; account.contacts.foreach(function(contact) { if (!subscribedaccounts[contact.accountid]) { subscribetoaccount(contact.accountid); subscribedaccounts[contact.accountid] = true; } }); // subscribed feed if (!subscribedaccounts[accountid]) { subscribetoaccount(accountid); } }); // remove listeners if socket disconnects socket.on('disconnect', function() { saccount.contacts.foreach(function(contact) { var eventname = 'event:' + contact.accountid; app.removeeventlistener(eventname, handlecontactevent); console.log('unsubscribing ' + eventname); }); app.triggerevent('event:' + accountid, { from: accountid, action: 'logout' }); }); var cookieparser = require('cookie-parser')(session_secret); // ### cookie parser // wrapper arround express cookie parser, can use same cookie parser socket.io. // parse cookie header , populate `socket.request.cookies` object keyed cookie names. // uses signed cookies passing secret string, assigns `socket.request.secret` may used other middleware. function cookieparserwrapper (socket, next) { // request, response , callback cookieparser(socket.request, {}, next); } // handle incoming chats client socket.on('chatclient', function(data) { sio.sockets.in(data.to).emit('chatserver', { from: accountid, text: data.text }); }); }); }); }
without testing code myself or anything.
"typeerror: cannot read property 'load' of undefined"
that particular error means data.sessionstore undefined , "load" not exists property, since there literally nothing defined in data.sessionstore.
so problem in opinion session system not working properly. hope helps bit!
Comments
Post a Comment