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

4 messages · Page 1 of 1 (latest)

thorn iron
#

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)

pseudo otterBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      ✅ You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)
thorn iron
#

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})

#

OOOOKAY ignore me. I forgot to put "return" in front of the NextResponse, like a moron.