Koa 莫名其妙报 ctx.onerror is not a function 是啥情况?

每当我本地打开 safiri 的时候,本地的 koa 项目就会崩溃,不知道是啥情况。。

Listening on:8000
/Users/lmn/project/u-pain-i-guess/node_modules/koa/lib/application.js:133
      const onerror = err => ctx.onerror(err);
                                 ^

TypeError: ctx.onerror is not a function
    at Array.onerror (/Users/lmn/project/u-pain-i-guess/node_modules/koa/lib/application.js:133:34)
    at listener (/Users/lmn/project/u-pain-i-guess/node_modules/on-finished/index.js:169:15)
    at onFinish (/Users/lmn/project/u-pain-i-guess/node_modules/on-finished/index.js:100:5)
    at callback (/Users/lmn/project/u-pain-i-guess/node_modules/ee-first/index.js:55:10)
    at ServerResponse.onevent (/Users/lmn/project/u-pain-i-guess/node_modules/ee-first/index.js:93:5)
    at emitNone (events.js:91:20)
    at ServerResponse.emit (events.js:188:7)
    at onFinish (_http_outgoing.js:592:10)
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)

上面这串是报错信息,server listen 在 8000 端口
我尝试在代码里进行添加

app
  .use(bodyParser)
  .use(router.routes())
  .use(router.allowedMethods())
  .on('error', console.error)

app.onerror = console.error

仍然这样。。
求大大解答。

阅读 9.7k
2 个回答

bodyParser用法错误

app
  .use(bodyParser)

替换为

app
  .use(bodyParser())

你应该把app.js里的代码拿出来看看的,不然谁也无法直接定位问题所在,你这种报错,可能是中间某段逻辑重写了app.context。

推荐问题
宣传栏