Next-connect error : unable to use next-connect for routing in my next.js project with javascript

976 views Asked by At

My next version is 13.4.5 (latest) My next-connect version is 1.0.0 (latest)

In pages>api>auth>signup.js :

import { createRouter } from "next-connect"

const handler = createRouter()

handler.post(async (req, res) => {
    res.send("Welcome from the signup page.")
})

export default handler

When I am making a post requesst using postman to - http://localhost:3000/api/auth/signup This is the error : Error [TypeError]: resolver is not a function

I am exporting the function correctly as you can see and I have also tried this code below :

import nc from "next-connect"

const handler = nc()

handler.post(async (req, res) => {
    res.send("Welcome from the signup page.")
})

export default handler

Now this is the error : Error [TypeError]: (0 , next_connect__WEBPACK_IMPORTED_MODULE_0__.default) is not a function

I want to get welcome ( "Welcome from the signup api" ) message as in the code and make the routing work properly with next connect

1

There are 1 answers

0
Riyan Ali On

I have solved the above error in the latest version of next-connect(pakage) if you have to use it with javasscript you have to use like this :

import { createRouter } from "next-connect"
const router = createRouter()
router.post(async (req, res) => {
    res.send("Hello, this is from the next-connect router")
})
export default router.handler()

Now send post request (using postman) according to the above code and it will work