0
users[name].emit('private_msg', {msg : msg, user:socket.username});

TypeError: users[name].emit is not a function

I am unable to solve this error, how can i fix this??

I am developing a chat application using nodejs and socket.io and here users{}; is an object which contains connected users, and I want to use them as socketid, to add functionality of private messaging.

full code :-

//Send Message
socket.on('send message',function(data,callback){
   var msg = data.trim();
    if(msg.substr(0,3) === "/p ")
        {
            msg = msg.substr(3);
            var ind = msg.indexOf(' ');
            if(ind !== -1)
                {
                    var name = msg.substring(0,ind);
                    var msg = msg.substring(ind + 1);
                    if(name in users)
                        {
                            console.log(users[name]);
                            users[name].emit('private_msg', {msg : msg, user:socket.username});
                            console.log('Prviate' + users[name]);
                        }
                    else
                        {
                            callback('Error!, user not found');
                        }
                }
            else
                {
                    callback('plz enter msg');
                }   
        }
    else
        {
            io.sockets.emit('new message',{msg : msg,user:socket.username});
        }
});
  • I'm not sure what you're expecting from posting this... – robertklep Apr 19 '17 at 16:37
  • 2
    Please provide more information so we can help you better. What is users?, for example. – Hosar Apr 21 '17 at 04:00
  • It sounds like you're not storing the actual socket in your `users` hash. You should show the code that populates the `users` hash. – Sunil D. Apr 23 '17 at 19:11
  • You need to provide a [mcve]. The code you've provided gives us no clue as to what `users` contains, so we have no way of knowing why you think `users[name].emit` exists. – Quentin Apr 24 '17 at 15:10

0 Answers0