next-connect TypeError: handlers[(i++)] is not a function

1.1k views Asked by At

I am trying to implement route->middleware->endpoint api approach in Next.js using 'next-connect' library. Everything was working fine until I added .post() endpoint to the next-connect insance.

// 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;

then I added .post() endpoint

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

and got this error TypeError: handlers[(i++)] is not a function.

The createUser function worked correctly when I tested it in another route.

Any suggestions? Could it be a 'next-connect' bug?

1

There are 1 answers

0
ABDULLOKH MUKHAMMADJONOV On BEST ANSWER

I found the issue. Actually I was importing createUser from a wrong file by mistake.

changed

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

to

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