Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
TypeError: Cannot read property 'room' of undefined from Javascript Mastery's Video

I am getting this error message, I tried everything, but I cannot understand the problem. This doubt is from Javascript Mastery's chat application video https://www.youtube.com/watch?v=ZwFA3YMfkoc&list=PLnVfUXqrBawfHoUBpnghPZed-TteNYBRn&index=4

Error Message:

TypeError: Cannot read properties of undefined (reading 'room')
at Socket. (C:\Users\Jatan\chat-app\server\index.js:43:20)
at Socket.emit (node:events:520:28)
at Socket.emitUntyped (C:\Users\Jatan\chat-app\server\node_modules\socket.io\dist\typed-events.js:69:22)
at C:\Users\Jatan\chat-app\server\node_modules\socket.io\dist\socket.js:465:39
at processTicksAndRejections (node:internal/process/task_queues:78:11)
[nodemon] app crashed - waiting for file changes before starting...

Index.js :

 socket.on('sendMessage', (message , callback) => {
        const user = getUser(socket.id)

        io.to(user.room).emit('message', {user:user.name, text: message})

        callback();
    })

Edit:

console.log(user) result:

[nodemon] starting `node index.js`
Server has started on port 5001
undefined
C:\Users\Jatan\chat-app\server\index.js:47
        io.to(user.room).emit('message', {user:user.name, text: message})
                   ^

TypeError: Cannot read properties of undefined (reading 'room')
    at Socket.<anonymous> (C:\Users\Jatan\chat-app\server\index.js:47:20)
    at Socket.emit (node:events:520:28)
    at Socket.emitUntyped (C:\Users\Jatan\chat-app\server\node_modules\socket.io\dist\typed-events.js:69:22)
    at C:\Users\Jatan\chat-app\server\node_modules\socket.io\dist\socket.js:465:39
    at processTicksAndRejections (node:internal/process/task_queues:78:11)
[nodemon] app crashed - waiting for file changes before starting...

result after console.log(socket.id):

Server has started on port 5001
3EQA0pQ5RuZenc8nAAAH
C:\Users\Jatan\chat-app\server\index.js:46
        io.to(user.room).emit('message', {user:user.name, text: message})
                   ^

TypeError: Cannot read properties of undefined (reading 'room')
    at Socket.<anonymous> (C:\Users\Jatan\chat-app\server\index.js:46:20)
    at Socket.emit (node:events:520:28)
    at Socket.emitUntyped (C:\Users\Jatan\chat-app\server\node_modules\socket.io\dist\typed-events.js:69:22)
    at C:\Users\Jatan\chat-app\server\node_modules\socket.io\dist\socket.js:465:39
    at processTicksAndRejections (node:internal/process/task_queues:78:11)
[nodemon] app crashed - waiting for file changes before starting...
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Finally got the solution.

Updated code:

const users = [];

const addUser = ({id,name,room}) => {
    //name = name.trim().toLowerCase();
    //room = room.trim().toLowerCase();

    const existingUser = users.find((user)=> user.name === name && user.room === room)

    if (existingUser) {
        return {error: 'Username is taken'}
    }

    const user = {id,name,room};

    users.push(user);

    return {user}
}

const removeUser = (id) => {
    const index = users.findIndex((user) => user.id === id)

    if (index !== -1){
        return users.splice(index, 1)[0]
    }
}

const getUser = (id) => {
    const user = users.find((user) => user.id === id)
    return user
}

const getUsersInRoom = (room) => {users.filter((user) => user.room === room)}

module.exports = { addUser, removeUser, getUser, getUsersInRoom}

Thanks everyone who tried to solve my problem.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!