0

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.

Renvo
  • 13
  • 3

1 Answers1

-1

When you declare a variable inside a function, it is considered a local variable to the function, and you can't access it from outside. you can solve this by declaring b as a global variable outside the function, and then changing it inside the function:

var b;
var io = require('socket.io')(serv,{});
io.sockets.on('connection', function(socket){
    console.log('new connection!');
    
    socket.on('clntMsg',function(data){
        b = eval(data);
        console.log(data);
    });


});
var a = b % 3;

But beware, you can only use the new value assigned to b after the function is called, so async stuff can cause issues.

atanay
  • 174
  • 10
  • 1
    Doesn't make a lot of sense to do this since b will be defined only after the callback is executed. Which means its going to give undefined error anyway. – Mythos Sep 10 '21 at 08:36
  • Yes, that's what I said in the warning, but since I don't know the whole structure of the asker's code, I just gave the general answer. – atanay Sep 10 '21 at 08:45
  • thanks, the error is gone. but there is a new problem. (described in a post) – Renvo Sep 10 '21 at 10:27
  • the new problem arises because the code tries to assign b%3 to "a" before b is assigned eval(data). the line "io.sockets.on(...)"; just defines the function, and waits for 'clntMsg's in the background. so "var a = b % 3;" runs just after the declaration, without b assigned. – atanay Sep 10 '21 at 12:15