project_chat_application icon indicating copy to clipboard operation
project_chat_application copied to clipboard

TypeError: Cannot read property 'room' of undefined

Open vickypotter opened this issue 4 years ago • 15 comments

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

TypeError: Cannot read property 'room' of undefined at Socket. (D:\React.js\Chat App\server\index.js:28:20) at Socket.emit (events.js:210:5) at D:\React.js\Chat App\server\node_modules\socket.io\lib\socket.js:528:12 at processTicksAndRejections (internal/process/task_queues.js:75:11) [nodemon] app crashed - waiting for file changes before starting...

vickypotter avatar Jul 05 '20 14:07 vickypotter

anyone pls give solution

vickypotter avatar Jul 05 '20 14:07 vickypotter

Encounter the same too @adrianhajdin

fonmagnus avatar Jul 08 '20 16:07 fonmagnus

I had the same issue, but found what was wrong.

In my case the getUser() function didn't returned a user, because I used curly braces, which doesn't return anything thus it was undefined. After I checked that function in server/users.js, deleted braces, and my chat is finally working.

CuriousWizard avatar Jul 20 '20 08:07 CuriousWizard

I had the same issue. I couldn't use sendMessage function because it returned that error. I tinkered around, and in my case, changing the const to let int the function when defining user solved the problem.

It would be great if someone would explain why it works this way, but it hey it works.

So in server/index.js

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

tamysiby avatar Aug 06 '20 12:08 tamysiby

Could be a typo. I had getUserInRoom as opposed to getUsersInRoom, using user instead of users. That's the problem I had. Also, restart the server manually whenever you make a change even if you're using nodemon. Lost a lot of time unnecessarily.

brodave318 avatar Aug 06 '20 18:08 brodave318

Getting this error as well. It happens when trying to restart the server. I think having some way to remember the rooms and then re-registering them to socket.io on restart might fix it. Thoughts?

nitinankad avatar Aug 19 '20 07:08 nitinankad

I'm also facing the same issue, what I think in my case is, when a user joins a chat room with a username that is already in use, no error is being generated, so user gets entry into the user interface, but also he isn't able to send any message, so the sendMessage function crashes at the time, crashing the server.

Yeah, it is the above-described issue, it is also present on the hosted application.

Dev-Sec0901 avatar Aug 23 '20 18:08 Dev-Sec0901

Getting this error as well. It happens when trying to restart the server. I think having some way to remember the rooms and then re-registering them to socket.io on restart might fix it. Thoughts?

Yeah, I agree with you. Whenever I try to refresh after joining room & sending few messages, the server crashes.It happens may be because after refreshing the 'disconnect' event if fired off!! If somehow we could remember the rooms, may be the problem will be fixed!!

shaan-alam avatar Sep 17 '20 05:09 shaan-alam

I sent name in Chat and that fixed

elisalimli avatar Sep 23 '20 01:09 elisalimli

@alisalim17 can you please share the code regarding the fix?

viswa9688 avatar Dec 30 '20 17:12 viswa9688

[ FIXED ] :

In server/users.js :

const getUser = (id) => users.find((user)=> user.id === id)

Remove the '{ }' after the '=>' , Code will work perfectly.

harshgarg1808 avatar Feb 21 '21 11:02 harshgarg1808

Follow the tutorial properly, you should not get this error.(saying this coz I spent a lot time figuring this error while there was a mistake in identifier name typed wrong by me ).

ankitdoot avatar Jul 30 '21 03:07 ankitdoot

[ FIXED ] :

In server/users.js :

const getUser = (id) => users.find((user)=> user.id === id)

Remove the '{ }' after the '=>' , Code will work perfectly.

This works.

MbuguaGeorge avatar Jan 25 '22 15:01 MbuguaGeorge

I am also getting the same issue. I checked everything and there is no typo in my code but still the issue continues.

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...

Jatan88 avatar Apr 29 '22 18:04 Jatan88

@vickypotter Have you got any solution?

Jatan88 avatar Apr 29 '22 19:04 Jatan88