【问题标题】:Node.js async.each throws "Callback was already called"Node.js async.each 抛出“回调已被调用”
【发布时间】:2016-11-22 17:45:29
【问题描述】:

这个问题可能看起来像许多其他问题的重复,但我在任何地方都找不到我的错误。

问题是 async.each 抛出“回调已被调用”。这是 sn-p(我将异步回调命名为 done,因此它不会与我代码中的其他回调混淆):

async.each(this.requirements, (requirement, done) => {
  // That thing here passes the result as a callback
  requirement.callback((result) => {
    if (!result) {
      // requirement not passed -> return error
      done(true); // LINE 42
    } else {
      done(); // LINE 44
    }
  }, data, params, bot);
}, (err) => {  // 'done' callback
  log.info('handler',
    `Handler '${this.label}' ${err ? 'failed' : 'succeeded'}`);
  // if any requirement did not pass, do not execute handler callback
  if (!err) this.callback(data, params, bot);
});

这是它的堆栈跟踪:

C:\Users\samuel\Code\node\sk22tgjs\node_modules\async\dist\async.js:837
          if (fn === null) throw new Error("Callback was already called.");
                           ^

Error: Callback was already called.
    at C:\Users\samuel\Code\node\sk22tgjs\node_modules\async\dist\async.js:837:34
    at requirement.callback (C:\Users\samuel\Code\node\sk22tgjs\node_modules\telegramjs\core\handler.js:44:11)
    at Requirement.exports.command.Requirement.callback (C:\Users\samuel\Code\node\sk22tgjs\node_modules\telegramjs\telegram\requires.js:21:5)
    at async.each (C:\Users\samuel\Code\node\sk22tgjs\node_modules\telegramjs\core\handler.js:39:19)
    at C:\Users\samuel\Code\node\sk22tgjs\node_modules\async\dist\async.js:2953:18
    at replenish (C:\Users\samuel\Code\node\sk22tgjs\node_modules\async\dist\async.js:872:19)
    at C:\Users\samuel\Code\node\sk22tgjs\node_modules\async\dist\async.js:878:27
    at C:\Users\samuel\Code\node\sk22tgjs\node_modules\async\dist\async.js:840:18
    at requirement.callback (C:\Users\samuel\Code\node\sk22tgjs\node_modules\telegramjs\core\handler.js:44:11)
    at Requirement.callback (C:\Users\samuel\Code\node\sk22tgjs\node_modules\telegramjs\core\requires.js:19:5)

有趣的是,只有在调用 done(true) 时才会出现此问题。然而,错误发生在第 44 行,而不是第 42 行。

你还可以在 GitHub 上看到损坏的代码,尤其是 testing 分支:https://github.com/22sk/telegramjs

提前致谢。

【问题讨论】:

标签: javascript node.js asynchronous callback


【解决方案1】:

当找不到命令时,成功和失败都会被调用,而不仅仅是失败。

https://github.com/22sk/telegramjs/blob/5b85f04fe890a8fd32b373edb97bfebc923156b1/bot/telegram/requires.js

exports.command = new Requirement({
  label: 'command',
  requires: requires.has('message', 'text'),
  callback: (result, data, params, bot) => {
    const command = new Command(data.message.text);
    if (!command.valid || command.bot && bot.me.username !== command.bot) {
      // command is not valid or not meant to be handled by this bot
      result(false); // <--- ### Missing return ### --->
    }
    // command is valid and should be handled by this bot
    // write command into data
    params.command = command;
    result(true);
  }
});

【讨论】:

    猜你喜欢
    • 2016-01-26
    • 2017-06-23
    • 2021-10-02
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 2018-09-14
    • 2023-03-21
    相关资源
    最近更新 更多