0

I am new to Javascript and got some errors. I can't figure out how to fix it.

I am adding a listener in here. I tried to add 'exports.' for access.

function AddListenerToBot(ChannelName)
{
    var CommandsListener = function Commands(from,message)
    {
        //snip
    }

    exports.CommandsListener = CommandsListener;

    bot.addListener('MessageCH'+ChannelName.toLowerCase(),CommandsListener);

}

It is working but when I need to remove it from another functions, I get an error

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received undefined

I can't access it.

Here is the removing function. They are in the same .js file.

 exports.RemoveListenerFroMBot = function(IncomingCommandText, CBValue)
    {  
            //snip
                        if (err)
                        {
                            console.log('Error', err);
                            CBValue('Error');
                        }
                        else
                        {
  bot.removeListener('IncomingCommandText.toLowerCase()',AddListenerToBot.CommandsListener);
                            console.log('Success');
                            CBValue('success);
                        }       
                    //snip
    }

Can someone help me to how can i remove listener from another function?

I looked them and can't find anything;
Node.js EventEmitter: How to bind a class context to the event listener and then remove this listener
Nodejs calling function from within a function
node.js event listener in another source file
How do I properly remove an event listener within a class?
removeEventListener of Anonymous function javaScript
Javascript removeEventListener not working
Javascript removeEventListener not working

Edit1: That's the Add function that using AddListenerBot() function.

exports.Add = function(ComingCB, cb)
{
        //snip
                    const jsonString = JSON.stringify(JSONData)
                    fs.writeFile('./Database/Ch.json', jsonString, err =>
                    {
                        if (err)
                        {
                            console.log('Err', err);
                            cb('Err');
                        }
                        else
                        {
                            console.log('Success');
                            cb('Success');
                            AddListenerToBot(ComingCB);                       
                        }
                    })
        //snip          
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Batur
  • 48
  • 1
  • 9
  • 1
    even if what you were trying to do made sense, ` exports.CommandsListener = CommandsListener;` isn't executed until `AddListenerToBot` is executed ... so the export couldn't happen until too late – Jaromanda X Jun 28 '21 at 11:24
  • Yes, you are right. Do you think should i move that exports after adding listener? – Batur Jun 28 '21 at 11:25
  • 1
    how would that work, since `CommandsListener` is defined inside `AddListenerToBot` ... I'm sure adding/removing listeners isn't that difficult - I just can't figure out what you're trying to do – Jaromanda X Jun 28 '21 at 11:27
  • In another function called Add(IncommingCommand,CBValue), i am getting parameters and do things in code then execute AddListenerToBot(param1,param2) function depends on the parameters from Add() function. Then get the values for using them in the CommandsListener function. After everything is finished i add listener to bot with function to work is proper. If its bad practice coding, can you help me to fix it? – Batur Jun 28 '21 at 11:31
  • I edited the post. Added the Add() function for better explanation. – Batur Jun 28 '21 at 11:36
  • In the the `bot.removeListener` call you reference `AddListenerToBot.CommandsListener`, but you don't seem to have defined that anywhere. Are you misunderstanding `exports`? – MikeM Jun 28 '21 at 11:57
  • Could be, what i was trying to do is CommandsListener function to make global in that file. So i thought that i can access it from anywhere (for example in bot.removeListener). I read some things about 'exports'. I thought that they were like make the functions global. – Batur Jun 28 '21 at 12:03
  • 1
    No, `exports` is used to export from the file, so try changing it to `AddListenerToBot.CommandsListener = CommandsListener;`. – MikeM Jun 28 '21 at 12:04

0 Answers0