【问题标题】:next-connect TypeError: handlers[(i++)] is not a functionnext-connect TypeError: handlers[(i++)] is not a function
【发布时间】:2021-11-16 18:29:30
【问题描述】:

我正在尝试使用“next-connect”库在 Next.js 中实现 route->middleware->endpoint api 方法。在我将 .post() 端点添加到下一个连接实例之前,一切正常。

// pages/api/index
import { protect, restrictTo, createUser } from 'api-lib/controllers/authController'
import { getAllUsers } from 'api-lib/controllers/userController'
import all from 'api-lib/middlewares/all';

const route = all() // next-connect instance with options privided

route.use(protect)             // rotect the route
     .use(restrictTo('admin')) // restrict the route to admin
     .get(getAllUsers)

export default route;

然后我添加了 .post() 端点

route.use(protect)             // rotect the route
     .use(restrictTo('admin')) // restrict the route to admin
     .get(getAllUsers)         // ---- works fine until here
     .post(createUser)         // !!! got error

得到了这个错误TypeError: handlers[(i++)] is not a function

createUser 函数在我在另一条路线中测试时正常工作。

有什么建议吗?会不会是“下一个连接”错误?

【问题讨论】:

    标签: next.js next-connect


    【解决方案1】:

    我发现了问题。实际上我是从错误的文件中错误地导入了createUser

    改变了

    // pages/api/index
    import { protect, restrictTo, createUser } from 'api-lib/controllers/authController'
    import { getAllUsers } from 'api-lib/controllers/userController'
    

    // pages/api/index
    import { protect, restrictTo } from 'api-lib/controllers/authController'
    import { getAllUsers, createUser } from 'api-lib/controllers/userController'
    

    【讨论】:

      猜你喜欢
      • 2021-03-14
      • 1970-01-01
      • 2018-08-23
      • 2020-08-09
      • 2021-11-19
      • 1970-01-01
      • 2016-09-14
      • 2021-10-08
      • 1970-01-01
      相关资源
      最近更新 更多