【发布时间】: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