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.

set onError function will get error:'next() called multiple times'

See original GitHub issue

Promblem/Feature Request Summary

set onError function will get error:‘next() called multiple times’

Environment Information

  • koa-body: “^2.5.0”
  • Koa: “^2.5.2”
  • Node.js: v8.11.1

Current Behavior

get follow error

 Error: next() called multiple times
application.js:190
      at dispatch (c:\Users\Administrator\Desktop\github-items\1-MyItems\ele-backend\node_modules\koa-router\node_modules\koa-compose\index.js:38:45)
      at next (c:\Users\Administrator\Desktop\github-items\1-MyItems\ele-backend\node_modules\koa-router\node_modules\koa-compose\index.js:45:18)
      at c:\Users\Administrator\Desktop\github-items\1-MyItems\ele-backend\node_modules\koa-body\index.js:98:14
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

Steps to Reproduce

router.post(
        '/upLoadHeadPicture',
        koaBody({
            multipart: true,
            onError: (error, ctx) => {
                ctx.body = {
                    code: 400,
                    error
                };
            },
            formidable: {
                uploadDir: './storage/img/userHeadImg',
                maxFieldsSize: 1
            }
        }),
        async (ctx) => {
            ctx.body = {
                code: 200
            };
        }
    );

1.i am testing the maxFieldsSize,i upload a file which file size lager than 1kb. 2.when no onError function,koa app just thorw a excceed size error message. 3.when i set the onError function to catch error,i get the error above.

Expected Behavior

Respone excceed size error message to client,and koa app get no error

Possible Solution

Maybe koa-router conflict with koa-body? i can’t find the solution.

Context (Environment)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
KleinMaximuscommented, Sep 7, 2018

koa-bodyparser solved my problem

0reactions
damianbcommented, Mar 7, 2019

It appears that where onError may be called, koa-body does not bail out after calling the onerror function and instead continues execution.

Of note:

https://github.com/dlau/koa-body/blob/8b0d77931ca2fdf031c0e37c34b372efc4d091a5/index.js#L96-L102

https://github.com/dlau/koa-body/blob/8b0d77931ca2fdf031c0e37c34b372efc4d091a5/index.js#L106-L113

This is likely a BC-breaker, but we should consider returning after calling the onError function (and NOT calling next() at all, as this is an error scenario)

Read more comments on GitHub >

github_iconTop Results From Across the Web

set onError function will get error:'next() called multiple times'
Promblem/Feature Request Summary set onError function will get error:'next() called multiple times' Environment Information koa-body: ...
Read more >
ios - RxSwift - onError emitted twice - Stack Overflow
I subscribe for this observer every time user hits Login Button. Is it approach correct? If I receive an error, next time when...
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 >
A Guide to Error Handling in Express.js | Scout APM Blog
A Guide to Error Handling in Express.js. Error handling often doesn't get the attention and prioritization it deserves.
Read more >
Using the middleware pattern in blue-rabbit - Blog
The middleware stack can be understood as a series of functions which ... return Promise.reject(new Error('next() called multiple times')) ...
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