简体   繁体   English

Node.js无法读取未定义错误的属性“发出”

[英]Nodejs Cannot read property 'emit' of undefined error

I'm trying to send message to specific user on nodejs, then save socket.id and nickname as username into array, then get that to send message, but I get this error: 我正在尝试将消息发送给nodejs上的特定用户,然后将socket.idnickname作为用户名保存到数组中,然后获取该消息以发送消息,但是出现此错误:

Cannot read property 'emit' of undefined

This is my code: 这是我的代码:

var socket      = require('socket.io'),
    express     = require('express'),
    app         = express(),
    server      = require('http').createServer(app),
    io          = socket.listen(server);

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

        socket.on('login', function (data) {
            login(data.username, data.password, function (success, value) {
                if (success) {
                    usernames[data.username] = socket.id;
                    socket.emit('login', {result: true, id: value});
                } else {
                    socket.emit('login', {result: false});
                }
            });
        });

        socket.on('requestMoney', function (username) {
            log.info('username: ' + username);
            usernames[username].emit('message', {username: 'Hey !!'});
        });
    });

usernames[username] is undefined for the given username , because of one or more of the following things: 由于以下一项或多项原因,未为给定的username定义usernames[username]

  1. Your given username does not match a data.username connected yet, and/or usernamedata.username尚未匹配,和/或
  2. You're trying to call emit on an number, not a socket ( usernames[data.username] = socket.id; ), and/or 你试图调用emit上一个号码而不是一个插座( usernames[data.username] = socket.id;和/或
  3. The usernames object is not in scope when you're trying to use it in the requestMoney event handler. 尝试在requestMoney事件处理程序中使用usernames对象时,该对象不在范围内。

I'd like to place a wager on #2. 我想下注#2。

    socket.on('login', function (data) {
        login(data.username, data.password, function (success, value) {
            if (success) {
                usernames[data.username] = socket.id;
                io.sockets.emit('login', {result: true, id: value});
            } else {
                io.sockets.emit('login', {result: false});
            }
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Nodejs发送给特定用户获取无法读取未定义错误的属性“发出” - Nodejs send to specific user get Cannot read property 'emit' of undefined error 无法读取 Nodejs 上未定义错误的属性“图像” - Cannot read property 'Image' of undefined error on Nodejs 无法读取nodejs中未定义错误的属性 - Cannot read property of undefined error in nodejs TypeError:无法读取未定义的属性“ emit” - TypeError: Cannot read property 'emit' of undefined “ TypeError:无法读取未定义的属性'$ emit'”-VueJS - “TypeError: Cannot read property '$emit' of undefined” - VueJS 无法读取Node.js中未定义的属性'then' - Cannot read property 'then' of undefined in nodejs 使用supertest在Nodejs上进行测试会给出“无法读取未定义的属性'header'”错误 - Testing on Nodejs with supertest gives “Cannot read property 'header' of undefined” error NodeJS执行错误TypeError:无法读取未定义的属性“主机” - NodeJS execution error TypeError: Cannot read property 'host' of undefined NodeJS Express 模块错误:TypeError:无法读取未定义的属性“已完成” - NodeJS Express Module Error: TypeError: Cannot read property 'finished' of undefined 使用React / Socket.IO无法读取未定义错误的属性'emit' - Cannot read property 'emit' of undefined error using React/Socket.IO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM