首页 > 解决方案 > nodejs.TypeError:下一个不是函数

问题描述

我是打字稿的新手。现在我想获取api响应信息(包括Elapsed time,,,response statusapi route。我刚刚在github response-time中找到了一个中间件。我只是在启动时简单地使用了这个中间件。

export default (app: Application) => {
  //some code here
  var responseTime = require('response-time')
  app.use(responseTime(function (req, res, time) {
    //some code here
  }))
  
};

当我npm run dev,我得到

2020-07-30 11:28:21,929 ERROR 85820 [-/127.0.0.1/-/1ms GET /favicon.ico] nodejs.TypeError: next is not a function
    at responseTime (/Users/lithium/develop/one-authentication-center/node_modules/response-time/index.js:61:12)
    at dispatch (/Users/lithium/develop/one-authentication-center/node_modules/koa/node_modules/koa-compose/index.js:42:32)
    at /Users/lithium/develop/one-authentication-center/node_modules/koa/node_modules/koa-compose/index.js:34:12
    at MidwayApplication.handleRequest (/Users/lithium/develop/one-authentication-center/node_modules/koa/lib/application.js:166:12)
    at MidwayApplication.handleRequest (/Users/lithium/develop/one-authentication-center/node_modules/egg/lib/application.js:211:18)
    at Server.handleRequest (/Users/lithium/develop/one-authentication-center/node_modules/koa/lib/application.js:148:19)
    at Server.emit (events.js:314:20)
    at parserOnIncoming (_http_server.js:777:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:119:17)

pid: 85820

我打开了 response-time/index.js,代码引发的错误是

  var fn = typeof opts !== 'function'
    ? createSetHeader(opts)
    : opts

  return function responseTime (req, res, next) {
    var startAt = process.hrtime()

    onHeaders(res, function onHeaders () {
      var diff = process.hrtime(startAt)
      var time = diff[0] * 1e3 + diff[1] * 1e-6

      fn(req, res, time)
    })

    next()
  }
}

标签: node.js

解决方案


你没有传递下一个 responseTime 而是一个字符串或日期/数字,我看到“时间”作为第三个不确定“这里的一些代码”做了什么,但似乎正试图按时调用下一个


推荐阅读