next-connect TypeError: handlers[(i++)] is not a function
next-connect TypeError: handlers[(i++)] is not a function
我正在尝试使用 'next-connect' 库在 Next.js 中实现路由->中间件->端点 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-connect' 错误?
我发现了问题。实际上我错误地从错误的文件导入 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'
我正在尝试使用 'next-connect' 库在 Next.js 中实现路由->中间件->端点 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-connect' 错误?
我发现了问题。实际上我错误地从错误的文件导入 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'