Search code examples
next.jsnext.js13

TypeError: responseHeaders.getSetCookie is not a function


I'm trying to learn more about Next and i'm following a very basic tutorial, but i'm running into a error

When doing a trycatch and made a simple post method, that just works fine when creating the topic, but instead of a 201 status, i receive a 500 status with no message, and in the console, this error shows up:

 ⨯ TypeError: responseHeaders.getSetCookie is not a function
    at new ResponseCookies (webpack-internal:///(rsc)/./node_modules/next/dist/compiled/@edge-runtime/cookies/index.js:213:47)
    at new NextResponse (webpack-internal:///(rsc)/./node_modules/next/dist/server/web/spec-extension/response.js:40:22)
    at NextResponse.json (webpack-internal:///(rsc)/./node_modules/next/dist/server/web/spec-extension/response.js:67:16)
    at GET (webpack-internal:///(rsc)/./app/api/topics/route.js:28:95)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async C:\Users\Micro\Desktop\Study\mongocrud\node_modules\next\dist\compiled\next-server\app-route.runtime.dev.js:6:62499

That's my trycatch connection:

const connectMongoDB = async () => {
    try {
        await mongoose.connect(process.env.MONGODB_URI)
        console.log('Connected to MongoDB')
    } catch (error) {
        console.log(error)
    }
}

And that's the post method

export async function POST(request){
    const {title, description} = await request.json()
    await connectMongoDB()

    await Topic.create({title, description})

    return NextResponse.json({message: "Topic Created"},{status: 201})
}

Seems to be something with next, but i have no idea how to fix it up


Solution

  • The TypeError: responseHeaders.getSetCookie is not a function error has been accidentally introduced in Next 13.5.5 as part of an internal upgrade of the runtime. The framework tries to call Headers#getSetCookie which older versions of Node prior to 18.16 don't ship and which isn't polyfilled anymore. As such that was an unintended breaking change.

    For now you can either downgrade Next to 13.5.4 or upgrade Node to at least 18.16. The Next team will be reverting that change soon and introduce it again in Next 14 where they plan to bump the minimum required Node version anyways.