【问题标题】:TypeError: Cannot call method 'get' of undefined at socket.handshakeTypeError:无法在 socket.handshake 调用未定义的方法“get”
【发布时间】:2015-01-05 18:40:35
【问题描述】:

我正在尝试将 socket.io 代码从 0.9v 重建到 1.x。 Express 和 socket.io 是最新版本。

所以,错误:Cannot call method 'get' of undefined at

 io.on('connection', function(socket) {
            console.log(socket.handshake);
            var username = socket.handshake.user.get('username'); //<----here

            ......

这是我的代码:

module.exports = function(server) {
                var io = require('socket.io')(server);
                io.set('origins', 'localhost:*');
            //I changed set to use, but it's doesn't matter i think
                io.use(function(socket, next) {  
                    var handshake = socket.request;
                //io.set('authorization', function(handshake, callback) {
                    async.waterfall([
                        function(callback) {
                         handshake.cookies = cookie.parse(handshake.headers.cookie || '');

     var sidCookie = handshake.cookies[config.get('session:key')];
      var sid = connect.utils.parseSignedCookie(sidCookie, config.get('session:secret'));
      //var sid = cookieParser.signedCookie(sidCookie, config.get('session:secret'));

                            loadSession(sid, callback);
                        },  ...continue...

继续:

function(session, callback) {

                            if (!session) {
                                callback(new HttpError(401, "No session"));
                            }

                            handshake.session = session;
                            loadUser(session, callback);
                        },
                        function(user, callback) {
                            if (!user) {
                                callback(new HttpError(403, "Anonymous session may not connect"));
                            }

                            handshake.user = user;
                            callback(null);
                        }

                    ], function(err) {.....});
                next();
                });

        io.on('connection', function(socket) { ... };

返回 io; };

好的,请帮帮我,谢谢!

【问题讨论】:

    标签: javascript node.js asynchronous socket.io authorize


    【解决方案1】:

    我将如何调试它。 无法对未定义的事物调用方法

    所以我们知道调用 .get 的东西还没有被创建。在这种情况下:

    var username = socket.handshake.user.get('username'); //<----here
    

    所以如果您无法访问 ide 调试器,我要做的第一件事是

    console.log(sockets)

    如果有数据

    console.log(socket.handshake);

    如果有数据

    console.log(socket.handshake.user);

    如果有数据检查它是否有方法 GET。

    无论如何,我的假设是 socket.handshake.user 尚未定义。 设置/公开它的代码在哪里?

    【讨论】:

    • 好的。我检查了控制台调试器“socket.handshake”——它有一些数据,但 socket.handshake.user 什么都没有(没有错误,只是新字符串)。和 socket.handshake.user.get - 无法读取未定义的属性“get”。
    • 设置/暴露它的代码在哪里? - 在function(user, callback) { handshake.user = user; callback(null); }
    • 好的。在function (user, callback){...} 之后handshake.user = user 我们有handshake.user.username = "correct username"。现在,我需要在io.on('connection', function(socket) { 中正确设置var username 帮助我!
    • 所以我可以看到你设置了user.username,但是你没有设置.get方法,也许我需要看一下socket方法,但是如果你亲自设置了用户对象,那么您将需要设置 get 函数
    • 你可以这样做来说明我的意思 handshake.user.username.get = (function(data){ console.log('hello' , data)});`
    猜你喜欢
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多