Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NextResponse error after upgrading to version 13.5.2 #55768

Closed
1 task done
Revaycolizer opened this issue Sep 21, 2023 · 6 comments
Closed
1 task done

NextResponse error after upgrading to version 13.5.2 #55768

Revaycolizer opened this issue Sep 21, 2023 · 6 comments
Labels
bug Issue was opened via the bug report template. stale The issue has not seen recent activity.

Comments

@Revaycolizer
Copy link

Link to the code that reproduces this issue

https://github.com/Revaycolizer/minimal-repo/tree/main/app/api

To Reproduce

  1. Clone repo
  2. next build
  3. See type error on GET route handler

Current vs. Expected behavior

Expected to return data but instead it throws an error stating that Promise<NextResponse | undefined>" is not a valid GET return type:

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Pro
    Binaries:
      Node: 16.17.0
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant Packages:
      next: 13.5.2
      eslint-config-next: 13.5.2
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.6
    Next.js Config:
      output: N/A

Which area(s) are affected? (Select all that apply)

App Router

Additional context

nexterror
I am deploying my application in vercel

@Revaycolizer Revaycolizer added the bug Issue was opened via the bug report template. label Sep 21, 2023
@hipdev
Copy link

hipdev commented Sep 21, 2023

Hello, in the last Next.js version apparently new rules were added, and you can no longer return undefined, the route API expects you to return "Response | Promise"
As the error mentions, you have an undefined in your GET route.
Just add a response in this last catch and that's it 👍

export async function GET(req: Request, res: Response) {
  try {
    const cookieStore = cookies()
    const token = cookieStore.get(SEARCHED)
    const posts = JSON.parse(token!.value)

    return NextResponse.json(posts)
  } catch (error) {
    return NextResponse.json(
      { error: 'Ouch, GET is not working my friend' },
      { status: 500 },
    )
  }
}

@gavhuang
Copy link

@hipdev thank you!

Adding a return in the catch block fixed it for me.

@Diwanshumidha

This comment was marked as off-topic.

@hipdev

This comment was marked as off-topic.

@Revaycolizer
Copy link
Author

It works @hipdev thanks

@nextjs-bot
Copy link
Collaborator

This issue has been automatically marked as stale due to two years of inactivity. It will be closed in 7 days unless there’s further input. If you believe this issue is still relevant, please leave a comment or provide updated details. Thank you.

@nextjs-bot nextjs-bot added the stale The issue has not seen recent activity. label Mar 21, 2025
@ztanner ztanner closed this as completed Mar 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. stale The issue has not seen recent activity.
Projects
None yet
Development

No branches or pull requests

6 participants