search

Home  >  Q&A  >  body text

TypeError in Next.js: Cannot read property of undefined (reading 'headers')

I encountered this error in the next js

TypeError: Cannot read property of undefined (read 'header') in eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:254:61)

Code snippet example:

newUser
    .save()
    .then(() =>
      NextResponse.json({ msg: "Successfuly created new User: " + newUser ,status:200})
    )
    .catch((err: string) =>{
    NextResponse.json({ error: "Error on '/api/register': " + err ,status:400})
    }
    );

Tried multiple approaches like importing NextApiRequest and NextApiResponse and using it. It has no status and JSON properties, basically it gives type error Property does not exist in NextApiResponse. So I used NextResponse directly and still got this error.

P粉146080556P粉146080556396 days ago878

reply all(1)I'll reply

  • P粉071602406

    P粉0716024062023-10-28 00:36:01

    I think you have to return NextResponse.json.

    This is what may cause this error.

    reply
    0
  • Cancelreply