Koa: TypeError: ctx.onerror is not a function

Created on 8 May 2018  路  6Comments  路  Source: koajs/koa

Environment

  • Nodejs: 8.11.1
  • Koa: 2.5.1
const onerror = err => ctx.onerror(err);
                               ^

TypeError: ctx.onerror is not a function
    at Array.onerror (./server/node_modules/koa/lib/application.js:147:32)
    at listener (./server/node_modules/on-finished/index.js:169:15)
    at onFinish (./server/node_modules/on-finished/index.js:100:5)
    at callback (./dist/server/node_modules/ee-first/index.js:55:10)
    at ServerResponse.onevent (./server/node_modules/ee-first/index.js:93:5)
    at emitNone (events.js:111:20)
    at ServerResponse.emit (events.js:208:7)
    at onFinish (_http_outgoing.js:720:10)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)

How to fix it?

  • Replace: const onerror = err => ctx.onerror(err);
  • With: const onerror = ctx.onerror;

paragraph removed by fl0w

question

Most helpful comment

I found out what my problem was.

const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const app = new Koa();

// I forgot to call the bodyParser module...
// app.use(bodyParser);

// better :)
app.use(bodyParser());

@und3fined perhaps this or something like this was your issue?

All 6 comments

Could you provide a full minimal reproducible example showcasing how this occurs?

Having the same issue.

  • Nodejs v8.9.4
  • Koa 2.5.1

I found out what my problem was.

const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const app = new Koa();

// I forgot to call the bodyParser module...
// app.use(bodyParser);

// better :)
app.use(bodyParser());

@und3fined perhaps this or something like this was your issue?

This is bug with bodyParser(koa-bodyparser) =))

I get the same error and fixed it with app.use(bodyParser())

I get the same error too and fixed it with app.use(bodyParser())

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ElegantScripting picture ElegantScripting  路  5Comments

rally25rs picture rally25rs  路  4Comments

felixfbecker picture felixfbecker  路  5Comments

rowild picture rowild  路  4Comments

usernameisalreadytaken2014 picture usernameisalreadytaken2014  路  4Comments