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
}