shubham
shubham

Reputation: 101

TypeError: Cannot read properties of undefined (reading 'headers') at eval Next js

I am having this error in next js

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

A sample of code snippet:

  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 approach like importing NextApiRequest and NextApiResponse and using it. It has no status and JSON properties in it basically it is giving types error property does not exist in NextApiResponse. So I am using NextResponse directly still getting this error.

Upvotes: 6

Views: 16439

Answers (2)

German Reynaga
German Reynaga

Reputation: 388

it just happened to me and I found the answer in a github issue that worked for me. Just add a return statement on each case

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

Upvotes: 9

Knee-co
Knee-co

Reputation: 493

I think you have to return the NextResponse.json.

That is what could be causing that error.

Upvotes: 38

Related Questions