我是 JavaScript 新手,遇到了一些错误。我不知道如何修复它。
我在这里添加了一个监听器。我试图添加“出口”。用于访问。
function AddListenerToBot(ChannelName)
{
var CommandsListener = function Commands(from,message)
{
//snip
}
exports.CommandsListener = CommandsListener;
bot.addListener('MessageCH'+ChannelName.toLowerCase(),CommandsListener);
}
它正在工作,但是当我需要从另一个函数中删除它时,它给出了 TypeError [ERR_INVALID_ARG_TYPE]:“listener”参数必须是函数类型。收到未定义。
我无法访问它
这里是删除功能。它们在同一个 .js 文件中。
exports.RemoveListenerFroMBot = function(IncommingCommandText, CBValue)
{
//snip
if (err)
{
console.log('Error', err);
CBValue('Error');
}
else
{
bot.removeListener('IncommingCommandText.toLowerCase()',AddListenerToBot.CommandsListener);
console.log('Success');
CBValue('success);
}
//snip
}
有人可以帮助我如何从另一个函数中删除侦听器吗?
我看了他们,找不到任何东西;
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
编辑 1: 这就是使用 AddListenerBot() 函数的 Add 函数。
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
}