简体   繁体   English

尝试调用API端点时,next不是函数

[英]Next is not a function when trying to call an API endpoint

Building an API in node.js with Koa and i'm getting an error TypeError: next is not a function I'm using koa and koa-mount to modularize my API. 使用Koa在node.js中构建API,并且出现错误TypeError: next is not a function我正在使用koakoa-mount模块化我的API的功能。 I have two files. 我有两个文件。

App.js : App.js

const Koa = require('koa')
const cors = require('@koa/cors')
const mount = require('koa-mount')
const logger = require('koa-logger')
const bodyParser = require('body-parser')
const price = require('./routes/price')
const margin = require('./routes/margin')
const position = require('./routes/position')


const main = async () => {

  const app = new Koa()

  // Parse incoming requests data
  app.use(bodyParser())
  app.use(logger())

  app.use(cors({
    credentials: true
  }))

  app.use(async (ctx, next) => {
    try {
      await next()
    } catch (err) {
      ctx.status = err.status || 500
      ctx.body = err.message
      ctx.app.emit('error', err, ctx)
    }
  })


  app.use(mount('/position', await position()))
  app.use(mount('/price', await price()))
  app.use(mount('/margin', await margin()))

  return app
}

if (require.main === module) {
  main().then(
    (app) => app.listen(3000), console.log(`Listening On Port ${3000}`)
  )
}

& the file including the endpoint 和包含端点的文件

// Import the WebFramework for routing
const Koa = require('koa')
const route = require('koa-route')
const bitmexAPI = require('../keys')

module.exports = async () => {
    const app = new Koa()

    app.use(route.get('/open', async (ctx) => {

        //Get Positions
        console.log("yes")

        const position = await bitmexAPI.Position.get()
        console.log(res)

        // //Response
        ctx.status = 200
        ctx.body = {
            tx: res.json({ success: true, data: position})    
        } 

    }))
    return app
}

It's meant to be a super simple API. 它的意思是成为一个超级简单的API。 I'm using postman to test this but it's returning a internal server error and the console is giving this out: 我正在使用postman对此进行测试,但它返回内部服务器错误,并且控制台正在发出此消息:

 TypeError: next is not a function
      at urlencodedParser (/Users/lrodriguez/Desktop/Personal/BitmexTracker/node_modules/body-parser/lib/types/urlencoded.js:91:7)
      at /Users/lrodriguez/Desktop/Personal/BitmexTracker/node_modules/body-parser/index.js:111:7
      at jsonParser (/Users/lrodriguez/Desktop/Personal/BitmexTracker/node_modules/body-parser/lib/types/json.js:110:7)
      at bodyParser (/Users/lrodriguez/Desktop/Personal/BitmexTracker/node_modules/body-parser/index.js:109:5)
      at dispatch (/Users/lrodriguez/Desktop/Personal/BitmexTracker/backend/node_modules/koa-compose/index.js:42:32)
      at /Users/lrodriguez/Desktop/Personal/BitmexTracker/backend/node_modules/koa-compose/index.js:34:12
      at Application.handleRequest (/Users/lrodriguez/Desktop/Personal/BitmexTracker/backend/node_modules/koa/lib/application.js:151:12)
      at Server.handleRequest (/Users/lrodriguez/Desktop/Personal/BitmexTracker/backend/node_modules/koa/lib/application.js:133:19)
      at Server.emit (events.js:189:13)
      at parserOnIncoming (_http_server.js:676:12)

我认为您使用的是Express的body-parser ,应该改用koa-bodyparser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM