0

I want to know how I can get this variable from outside the function, maybe it seems something very simple, but I have not been able to do it. I need the TRADE_OUT data to be able to be used globally in the application, but I can only use it within that function that detects it, below I made a small "detect" to check if it obtained the bariable but the value never does not change,

I have the following code:

 client.on("message", function(msg) {
        const prefix = '#';
        
      if (!msg.content.includes(prefix)) return;
    
    
    const pattern = new RegExp(prefix + '([a-z]+)', 'i');
    const getMatch = str => str.match(pattern)?.[1];
    msg.channel.send(getMatch(msg.content));
    var TRADE_OUT = getMatch(msg.content)

    console.log(TRADE_OUT)

    });

    if(TRADE_OUT != undefined){
        console.log(TRADE_OUT);
    }else{
        console.log("NOP")
    }

How do I detect that new TRADE_OUT outside the function in nodejs or javascript?

Jabibi
  • 35
  • 2
  • 7
  • is TRADE_OUT a constant ? – midugh Mar 14 '21 at 20:37
  • TRADE_OUT is a unique data that is taken from a message, I suppose it is not a constant since it is data that can save any value that is sent to it. But I'm not really sure, if you know please I need help <3 – Jabibi Mar 14 '21 at 20:43

2 Answers2

1

There is what we call scopes. It's all what's in curly brackets {}, so in functions, etc...

You can search on google what's the difference between var and let which are pretty similar, except for scope usage.

To access to a variable outside the current scope you want to, then you have to declare it outside this scope, or either pass it as parameter in your functions.

so here's a scope :

// declare it outside the event handler function's scope :
let TRADE_OUT = null;

client.on('message', function(msg) => {
    // function scope
    TRADE_OUT = getMatch(msg.content); // just modify its value here
});

You also should search the difference between function() {} and () => {} because there's scope's differences too.

To be accessible on the whole application, you'll need in NodeJS to use module.exports, for example like this :

module.exports.TRADE_OUT = TRADE_OUT;
NoxFly
  • 179
  • 1
  • 12
  • Up to this point I understand it, but I need that TRADE_OUT of the function to replace the null value of the declaration and to be able to use it outside the function, is it possible? – Jabibi Mar 14 '21 at 20:59
  • yeah, I declared it as null, but when the .on('message' event is triggered, then TRADE_OUT change its value. If you need it on other files, then don't declare TRADE_OUT, but `module.export.TRADE_OUT` and modify it. `module.export` is a special nodejs object where you can assign variables and functions etc... While you declare it outside of the function, it's okay – NoxFly Mar 14 '21 at 21:27
0

Simplified question:

client.on('foo', () => {

  const TRADE_OUT = 5;

});

If want '5' to accessible outside the event handler, you need to use a variable that is defined outside of the event handler;

let TRADE_OUT = null;

client.on('foo', () => {

  TRADE_OUT = 5;

});

The general thinking is, if you need information to be available between multiple scopes, the information needs to be defined in a scope (or object) that is reachable by all.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • yeah but it won't be accessible from the whole app but only from the file – midugh Mar 14 '21 at 20:44
  • Then you have to find a scope that's even larger, for example an exported module or a variable on a global object. – Evert Mar 14 '21 at 20:53