Next.js Discord

Discord Forum

fetch POST to api gives Cannot read properties of undefined headers response when parse response

Answered
Horse guard wasp posted this in #help-forum
Open in Discord
Avatar
Horse guard waspOP
for some reason this doesn't work when trying to parse the return json body object
  const validateUsername = () => {
    fetch("/api/user/validate", {
      method: "POST",
      body: JSON.stringify({
        validating: "username",
        username: registerUsername
      })
    })
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(err => console.error(err.statusText))
  }

gives this error: - error 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:277:61)
Answered by Horse guard wasp
OOOOKAY ignore me. I forgot to put "return" in front of the NextResponse, like a moron.
View full answer

2 Replies

Avatar
Horse guard waspOP
The error is no longer produced when removing the line that tries to parse the response object with .json, but then I cannot parse the body object and get what I want back. On the API side, here is what the response object looks like: NextResponse.json({message: "not found"}, {status: 200})
Avatar
Horse guard waspOP
OOOOKAY ignore me. I forgot to put "return" in front of the NextResponse, like a moron.
Answer