New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Uncaught TypeError: Cannot call method 'push' of undefined" error when wrapping scripts inside a function #497
Comments
I get this too, would be interested to know the correct way to load socket.io with require.js. |
Yep, me too. |
@maritz Having brought this up on the requirejs mailing list, it seems the only way to do it currently is either use wrap:false or change the line of code above yourself. |
Yeah, for the time being I just set my rjs config to
And then I load the socket.io/socket.io as a normal request. I don't like modifying third party libs in my depoyment, so this works best for me. |
I found the solution! ) var xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost:3001/socket.io/socket.io.js", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
eval.call(window, xhr.response);
var socket = io.connect('http://localhost:3001');
socket.on('poll', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
}
}
xhr.send(); That is eval.call(window, xhr.responseText) or with(window){
eval(xhr.responseText)
} |
same issue here.... as @sampsasaarela said, changing it to: })('object' === typeof module ? module.exports : io, this); worked for me. anyone working on integrating it? |
thx, I use this method solve this problem |
Thanks so much, this has helped me out greatly. |
So to summarise, the problem is that it assumes it is running in the global context (when executed in a browser). Making any delivery through a closure or timed execution break in unexpected ways when it turns out that the first variable declaration was just a local variable. I think this has been refactored in the master branch for 1.0.x, but for those still connecting to backends using the 0.9.x protocol (e.g. python gevent socketio) and using socket.io.js 0.9.16, here's another way to fix it. Change /*! Socket.IO.js build:0.9.16, .. */
var io = ('undefined' === typeof module ? {} : module.exports); to /*! Socket.IO.js build:0.9.16, .. */
io = ('undefined' === typeof module ? {} : module.exports); or /*! Socket.IO.js build:0.9.16, .. */
window.io = ('undefined' === typeof module ? {} : module.exports); |
I think that line 107 in file https://github.com/LearnBoost/socket.io-client/blob/master/dist/socket.io.js#L107 should change like this:
})('object' === typeof module ? module.exports : (this.io = {}), this);
->
})('object' === typeof module ? module.exports : io, this);
I use reguirejs (and almond.js) to pack all my clientside code to one file and I configured requirejs packer with wrap:true. So all my code is wrapped like (function(){/* mycode */}()). After making this change my wrapped codes will work, but without it, I get the following error
the line which cause error is
The text was updated successfully, but these errors were encountered: