question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: next() called multiple times

See original GitHub issue

Hello,

when defined multiple http status codes in output error is thrown.

  1. when only “400-599” is defined then ot works
  2. when “400-599” and 200 are defined the error is thrown.

It seams that it has issues wtih multiple status codes.

const joiSchema = {
  body: {
    email: Joi.string()
      .lowercase()
      .email()
      .max(45)
      .required(),
    password: Joi.string()
      .max(10)
      .required()
  },
  output: {
    200: {
      body: {
        jwtToken: Joi.string()
          .alphanum()
          .min(3)
          .max(200)
          .required()
      }
    },
    "400-599": {
      body: {
        error: Joi.object()
      }
    }
  },
  type: "json",
  continueOnError: true
};

Error: next() called multiple times at dispatch (/home/kresimir/Development/zimmerfrei/zimmerfrei-http-server/node_modules/koa-router/node_modules/koa-compose/index.js:38:45) at next (/home/kresimir/Development/zimmerfrei/zimmerfrei-http-server/node_modules/koa-router/node_modules/koa-compose/index.js:45:18) at errorHandler (/home/kresimir/Development/zimmerfrei/zimmerfrei-http-server/node_modules/koa-joi-router/joi-router.js:285:22) at process._tickCallback (internal/process/next_tick.js:68:7)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rolivarescommented, Mar 27, 2020

I my case same error is appearing when continueOnError: true but the error is not informed by Joi for example an undefined var on handler body

handler : async () => {
   // const a = 1
  await otherTier.myFunction(a)
}

So, I’ve simple add the validation (for testing) if the error is a joi error in order to continue whenever joi use continueOnError === true, otherwise the error must be thrown

if (!(err.isJoi && spec.validate.continueOnError)) return ctx.throw(err);

if (err.isJoi && spec.validate.continueOnError) {
        return await next();
      } else {
        return ctx.throw(err);
      }
0reactions
mdpxcommented, Jul 3, 2020

The problem is that when you throw an error (either in code explicitly or by runtime) in the route handler, the errorHandler catch the error and calls next() which breaks koa’s compose function. Although “an undefined var on handler body” is not a good example, @rolivares 's workaround works for custom errors, which is my case, but won’t work if I throw the Joi error from ctx.invalid as-is, which maybe nobody would really do that, it still should be taken into consideration IMO.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unhandled rejection TypeError: `next()` called multiple times ...
I think this is happening because you have nested then blocks. In your failure case, returning the result of done() in a then...
Read more >
How to prevent disasters caused by the callback getting called ...
In this situation execute() is the function of the third party module which I am using, and this callback gets called multiple times...
Read more >
Why Is My Future/Async Called Multiple Times? - Flutter Igniter
⁑ callAsyncFetch() gets invoked once per build returning new Future s every time; = didUpdateWidget in the FutureBuilder compares old and new Future...
Read more >
MongoDB Compass GUI - Callback called multiple times
I had the same problem as you. Here is an example of how to solve it: [ { "BMW" : ["BMW1", "BMW2", "BMW3"],...
Read more >
Events | Node.js v19.3.0 Documentation
EventEmitter; Event listener; EventTarget error handling; Class: Event ... then removeListener() must be called multiple times to remove each instance.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found