【问题标题】:simple social network using node.js and mongodb使用 node.js 和 mongodb 的简单社交网络
【发布时间】:2015-01-30 02:51:11
【问题描述】:

我正在尝试构建简单的社交网络,我正在关注这本书(使用 MongoDB 和 Backbone 构建节点应用程序)(https://github.com/Swiftam/book-node-mongodb-backbone/tree/master/ch10)。但是,我才发现node.js版本已经更新了。

我想解决一些问题,但是我在 chat.js 中遇到问题,指出这是错误:

ch10/routes/chat.js:27 data.sessionStore.load(data.sessionID, function(err, session) { TypeError:无法读取未定义的属性“加载”
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 to see if the account is 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(); });
        // Immediately trigger the login event
        // of this 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 to ' + eventName);
        };

        // Find the account contacts and 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 to my feed as well
            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 from ' + eventName);
            });
            app.triggerEvent('event:' + accountId, {
                from: accountId,
                action: 'logout'
            });
        });
                      var cookieParser = require('cookie-parser')(SESSION_SECRET);    

            // ### Cookie parser

                // Wrapper arround Express cookie parser, so we can use the same cookie parser for socket.io.
                // Parse Cookie header and populate `socket.request.cookies` with an object keyed by the cookie names.
                // Uses signed cookies by passing a secret string, which assigns `socket.request.secret` so it may be used by other middleware.

          function cookieParserWrapper (socket, next) {
            // request, response and callback
          cookieParser(socket.request, {}, next);
            }


        // Handle incoming chats from client
        socket.on('chatclient', function(data) {
            sio.sockets.in(data.to).emit('chatserver', {
                from: accountId,
                text: data.text
            });
        });
    });

});

}

【问题讨论】:

    标签: node.js mongodb backbone.js


    【解决方案1】:

    无需自己测试代码或其他任何东西。

    "TypeError: 无法读取未定义的属性 'load'"

    该特定错误意味着 data.sessionStore 未定义并且“加载”不作为属性存在,因为在 data.sessionStore 中实际上没有定义任何内容。

    所以我认为问题在于您的会话系统无法正常工作。希望对您有所帮助!

    【讨论】:

    • 谢谢,但是记忆库,我评论了,因为它是在新的node.js版本中解密的。那么你能告诉我如何修复 sessionStore 吗? //var MemoryStore = require('connect').session.MemoryStore; //app.sessionStore = new MemoryStore();
    • 此链接应为您指明正确的方向。 stackoverflow.com/questions/7420051/…
    猜你喜欢
    • 2019-06-26
    • 1970-01-01
    • 2011-05-15
    • 2013-08-22
    • 2013-10-21
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多