Console.log返回未定义和正确的值

时间:2019-06-22 12:17:25

标签: javascript node.js socket.io

我正在使用socket.io学习Node.js。在命令行中的基本聊天中,我想列出连接到服务器的套接字。

我的方法是在哈希图中存储几个套接字ID /用户名。当用户键入/ls时,它将向服务器发送一条消息,该消息将触发一个处理程序,该处理程序会将哈希图发送给用户,然后再显示。

客户端:

function chat_command(cmd, arg) {
    switch (cmd) {
.
.
.
        case 'ls':
            socket.emit('send', {sender: nick ,action: 'list'});
            break;
.
.
.

socket.on('message', function (data) {

.
.
.
    // List all connected clients
    else if(data.action === 'list'){
        console.log(data.list, 'bug');
        rl.prompt(true);
    }
.
.
.

服务器端:


io.sockets.on('connection', function (socket) {

    socket.on('send', function (data) {

        if (data.type === 'notice') { //update the hasmap with the name
            connectedSockets[socket.id] = data.nick;
            // console.log(connectedSockets);
        }

        if (data.action === 'broadcast') {
            io.sockets.emit('broadcast', data);
        }

        if (data.action === 'list') {
            console.log(connectedSockets , 'ok')
            socket.emit('message', {action:'list', list: connectedSockets});

        }
        else{
            io.sockets.emit('message', data);
        }
    });

除了客户端首先将哈希图显示为未定义然后正确显示,而在socket.emit之前的console.log确认哈希图已定义并填充之外,它的工作原理很好。它返回以下内容:

> undefined bug
> { bjHo78hGv9QJbElQAAAB: 'hello' } bug

代替

> { bjHo78hGv9QJbElQAAAB: 'hello' } bug

一种快速的解决方法是替换

if (data.action === 'list')

作者

if (data.action === 'list'&& data.list !== undefined)

但是我仍然不明白它会先打印undefined然后再显示哈希图。当哈希映射从未从未被定义时,如何在不同的值上执行两次代码。这真让我烦恼!我缺少明显的东西吗?

任何见解将受到高度赞赏。谢谢。

编辑:这是一个console.trace,可能对某人有用。

Trace: undefined bug
    at Socket.<anonymous> (/Users/soyanchardon/Documents/GitHub/iotProject/td1-Nanoboss/startercode/client side/client.js:87:17)
    at Socket.Emitter.emit (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/component-emitter/index.js:133:20)
    at Socket.onevent (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/socket.io-client/lib/socket.js:278:10)
    at Socket.onpacket (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/socket.io-client/lib/socket.js:236:12)
    at Manager.<anonymous> (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/component-bind/index.js:21:15)
    at Manager.Emitter.emit (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/component-emitter/index.js:133:20)
    at Manager.ondecoded (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/socket.io-client/lib/manager.js:345:8)
    at Decoder.<anonymous> (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/component-bind/index.js:21:15)
    at Decoder.Emitter.emit (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/component-emitter/index.js:133:20)
    at Decoder.add (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/socket.io-parser/index.js:251:12)

Trace: { q6iF1NWEoiagjYwaAAAA: 'max', FudzkqUMYrrvp_u7AAAB: 'ben' } bug
    at Socket.<anonymous> (/Users/soyanchardon/Documents/GitHub/iotProject/td1-Nanoboss/startercode/client side/client.js:87:17)
    at Socket.Emitter.emit (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/component-emitter/index.js:133:20)
    at Socket.onevent (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/socket.io-client/lib/socket.js:278:10)
    at Socket.onpacket (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/socket.io-client/lib/socket.js:236:12)
    at Manager.<anonymous> (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/component-bind/index.js:21:15)
    at Manager.Emitter.emit (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/component-emitter/index.js:133:20)
    at Manager.ondecoded (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/socket.io-client/lib/manager.js:345:8)
    at Decoder.<anonymous> (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/component-bind/index.js:21:15)
    at Decoder.Emitter.emit (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/component-emitter/index.js:133:20)
    at Decoder.add (/Users/soyanchardon/Documents/GitHub/iotProject/node_modules/socket.io-parser/index.js:251:12)

0 个答案:

没有答案