【问题标题】:Error composing koa async function middleware编写 koa 异步函数中间件时出错
【发布时间】:2017-01-18 16:08:35
【问题描述】:

我正在使用koa-compose 来按照guide 中的建议组合中间件。我将koa-compose 导入为kompose

我有以下代码:

const home = async function home (ctx,next) {
  if(ctx.path === '/') {
    ctx.body = 'Hello World!'
  } else {
    await next()
  }
}

const random = async function random (ctx,next) {
  console.log(ctx)
  if(ctx.path === '/random') {
    console.log('Inside random function')
    ctx.body = Math.floor(Math.random() * 10)
  } else {
    await next()
  }
}

const backwards = async function backwards (ctx,next) {
  if(ctx.path === '/backwards') {
    ctx.body = 'sdrawkcab'
  } else {
    await next()
  }
}

const pi = async function pi (ctx,next) {
  if(ctx.path === '/pi') {
    ctx.body = String(Math.PI)
  } else {
    await next()
  }
}

const body2 = kompose([random,backwards,pi,home])

我将它用作链中的最后一个中间件:

app.use(responseTime())
app.use(logger())
app.use(body2)
app.listen(3000)

我收到此错误:

TypeError: undefined is not a function
      at Object.<anonymous> (/home/vamsi/Do/koa-tutorial/node_modules/koa-compose/index.js:28:19)
      at undefined.next (native)
      at onFulfilled (/home/vamsi/Do/koa-tutorial/node_modules/co/index.js:65:19)
      at /home/vamsi/Do/koa-tutorial/node_modules/co/index.js:54:5
      at new Promise (/home/vamsi/.nvm/v6.2.0/lib/node_modules/babel-cli/node_modules/core-js/modules/es6.promise.js:191:7)
      at Object.co (/home/vamsi/Do/koa-tutorial/node_modules/co/index.js:50:10)
      at converted (/home/vamsi/Do/koa-tutorial/node_modules/koa-convert/index.js:17:15)
      at dispatch (/home/vamsi/Do/koa-tutorial/node_modules/koa/node_modules/koa-compose/index.js:43:32)
      at next (/home/vamsi/Do/koa-tutorial/node_modules/koa/node_modules/koa-compose/index.js:44:18)
      at _callee7$ (index.js:72:11)

完整代码为on github。您可以在那里的代码中将body() 替换为body2

更新:

在做了一些额外的日志记录后,它说:

 TypeError: next is not a function
       at Object._callee3$ (index.js:29:11)
       at tryCatch (/home/vamsi/.nvm/v6.2.0/lib/node_modules/babel-cli/node_modules/regenerator-runtime/runtime.js:62:40)
       at GeneratorFunctionPrototype.invoke [as _invoke] (/home/vamsi/.nvm/v6.2.0/lib/node_modules/babel-cli/node_modules/regenerator-runtime/runtime.js:336:22)
       at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/home/vamsi/.nvm/v6.2.0/lib/node_modules/babel-cli/node_modules/regenerator-runtime/runtime.js:95:21)
       at step (index.js:3:1)
       at index.js:3:1
       at new Promise (/home/vamsi/.nvm/v6.2.0/lib/node_modules/babel-cli/node_modules/core-js/modules/es6.promise.js:191:7)
       at Object.<anonymous> (index.js:3:1)
       at Object.backwards (index.js:25:7)
       at Object.<anonymous> (/home/vamsi/Do/koa-tutorial/node_modules/koa-compose/index.js:25:28),

【问题讨论】:

    标签: javascript node.js async-await koa koa2


    【解决方案1】:

    您正在为 Koa 2 使用基于生成器的 koa-compose,这是行不通的。你需要koa-compose@3.x 安装它:

    npm install -S koa-compose@next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-24
      • 2021-12-19
      相关资源
      最近更新 更多