koa-body
koa-body copied to clipboard
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: "^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)
I have the same problem. If I add onError
option the console shows Error: next() called multiple times
koa-bodyparser solved my problem
The same problem
Solved. If you have an error handler, there will throw this error. Use you error handler for error handling instead.
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)