Recently I started to study the node.js, and faced the "ReferenceError: b is not defined" problem. With some simple manipulation, I get the variable (b) inside the socket function. However, I calculate the received information outside the function. In this case, the program cannot figure out where to get the variable (b). I tried using variable return, but it didn't work. When I work with variable (b) inside a function, there are no problems. Tell me how to correctly solve this problem.
var io = require('socket.io')(serv,{});
io.sockets.on('connection', function(socket){
console.log('new connection!');
socket.on('clntMsg',function(data){
var b = eval(data);
console.log(data);
return b;
});
});
var a = b % 3;
Thank you, the error is gone, and everything works correctly in this form.
var io = require('socket.io')(serv,{});
var b
var a
io.sockets.on('connection', function(socket){
console.log('new connection!');
//var a = b % 3;
socket.on('clntMsg',function(data){
b = eval(data);
a = b % 3;
console.log(data);
socket.emit('serverMsg',{
msg:(a),
});
});
});
But as soon as I try to do this:
var io = require('socket.io')(serv,{});
var b
io.sockets.on('connection', function(socket){
console.log('new connection!');
socket.on('clntMsg',function(data){
b = eval(data);
console.log(data);
});
socket.emit('serverMsg',{
msg:(a),
});
});
var a = b % 3;
Then on the client side "null" is issued.