我试图使用'next-connect‘库在Next.js中实现路由->中间件->端点api方法。在我将.post()
端点添加到下一个--connect之前,一切都很好。
// 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++)不是一个函数。
当我在另一条路径中测试createUser
函数时,它正确地工作了。
有什么建议吗?会不会是“下一个连接”的错误?
发布于 2021-09-23 23:54:52
我发现了问题。实际上,我错误地从一个错误的文件导入了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'
https://stackoverflow.com/questions/69298620
复制