为什么回调中的套接字没有定义?
main.js:26078 Uncaught TypeError: Cannot read property 'on' of undefined控制台日志是一个套接字对象。
var jwt = sessionStorage.token;
console.log(socket);
socket.on('connect', function (socket) { // undefined socket
socket.on('authenticated', function () {
//do other things
})
.emit('authenticate', {token: jwt}); //send the jwt
});发布于 2016-04-24 14:22:36
假设这是客户端代码,没有传递给socket事件的connect参数,所以从代码中删除它。通过将该参数命名为socket,您将重写较高作用域的socket变量,并且由于没有任何传递给该事件,因此被重写的socket变量将是undefined。改为:
var jwt = sessionStorage.token;
console.log(socket);
// remove socket argument from this next callback
socket.on('connect', function () {
socket.on('authenticated', function () { // undefined socket
//do other things
})
.emit('authenticate', {token: jwt}); //send the jwt
});https://stackoverflow.com/questions/36822527
复制相似问题