project_chat_application
project_chat_application copied to clipboard
TypeError: Cannot read property 'room' of undefined
io.to(user.room).emit('message', {user:user.name, text:message}); ^
TypeError: Cannot read property 'room' of undefined
at Socket.
anyone pls give solution
Encounter the same too @adrianhajdin
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.
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);
...
});
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.
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?
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.
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!!
I sent name in Chat and that fixed
@alisalim17 can you please share the code regarding the fix?
[ FIXED ] :
In server/users.js :
const getUser = (id) => users.find((user)=> user.id === id)
Remove the '{ }' after the '=>' , Code will work perfectly.
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 ).
[ FIXED ] :
In server/users.js :
const getUser = (id) => users.find((user)=> user.id === id)
Remove the '{ }' after the '=>' , Code will work perfectly.
This works.
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.
@vickypotter Have you got any solution?