收到TypeError:无法读取未定义的属性“房间”

时间:2020-07-31 10:58:52

标签: javascript node.js express websocket socket.io

TypeError:无法读取Socket.socket.on上未定义的属性“ room”。 尽管代码对我来说看起来不错,但仍会生成类型错误。难道是因为Socket的版本?请告诉我们需要做什么来解决它。这是显示错误的代码段:

socket.on('joinRoom', ({username, room}) => {

        const user = userJoin(socket.id, username, room);

        socket.join(user.room);
        socket.emit('message', formatMessage(botName, 'Welcome to Concord!'));
        socket.broadcast.to(user.room).emit(
            'message', 
            formatMessage(botName, `${user.username} has joined the chat!`));

        io.to(user.room).emit('roomUsers' , {
            room: user.room,
            users: getRoomUsers(user.room)
        });
});

0 个答案:

没有答案