socketio / socket.io

Realtime application framework (Node.JS server)
https://socket.io
MIT License
61.05k stars 10.11k forks source link

Cannot call method 'apply' of undefined #1886

Closed hems closed 9 years ago

hems commented 9 years ago
send = io.sockets.send
send 'something'
/Users/hems/git/visitoutpost/weblg-midi/node_modules/socket.io/lib/namespace.js:240
  this.emit.apply(this, args);
            ^
TypeError: Cannot call method 'apply' of undefined
  at Namespace.send.Namespace.write (/Users/hems/git/visitoutpost/weblg-midi/node_modules/socket.io/lib/namespace.js:240:13)
  at module.exports (/Users/hems/git/visitoutpost/weblg-midi/app/funktions/some-file.coffee:10:2)
  at NodeMidiInput.inputs (/Users/hems/git/visitoutpost/weblg-midi/app/start.coffee:43:3)
  at NodeMidiInput.emit (events.js:98:17)

make: *** [start] Error 8
yads commented 9 years ago

You're not calling the function in the context of the namespace, either call it:

io.sockets.send('something'); or use call

var send = io.sockets.send;
send.call(io.sockets, 'something');
nkzawa commented 9 years ago

@yads thx for your reply!

hems commented 9 years ago

@yads lol yeah i know what i'm doing. i just thought you guys did not want to keep this behaviour as it quite odd on my personal point of view.

but if it's a "design choice", then cool.

i believe function like that should be "binded" to it's scope to avoid this kind of old school javascript issues.