TypeError in Next.js: Cannot read property of undefined (reading 'headers')
P粉146080556
P粉146080556 2023-10-27 10:05:19
0
1
876

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粉146080556
P粉146080556

reply all(1)
P粉071602406

I think you have to return NextResponse.json.

This is what may cause this error.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template