Next.js Discord

Discord Forum

getting params in route handler

Answered
American Chinchilla posted this in #help-forum
Open in Discord
American ChinchillaOP
- error TypeError: Cannot read properties of undefined (reading 'guild_id')
    at GET (webpack-internal:///(rsc)/./app/api/auth/server-auth/route.ts:9:125)
    at eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:254:43)
    at eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/lib/trace/tracer.js:111:36)
    at NoopContextManager.with (webpack-internal:///(rsc)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:360:30)
    at ContextAPI.with (webpack-internal:///(rsc)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:30:58)
    at NoopTracer.startActiveSpan (webpack-internal:///(rsc)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:953:34)
    at ProxyTracer.startActiveSpan (webpack-internal:///(rsc)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:993:36)
    at eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/lib/trace/tracer.js:100:107)
    at NoopContextManager.with (webpack-internal:///(rsc)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:360:30)
    at ContextAPI.with (webpack-internal:///(rsc)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:30:58)
    at NextTracerImpl.trace (webpack-internal:///(rsc)/./node_modules/next/dist/server/lib/trace/tracer.js:100:32)
    at eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:242:53)
    at AsyncLocalStorage.run (node:async_hooks:341:14)
    at Object.wrap (webpack-internal:///(rsc)/./node_modules/next/dist/server/async-storage/static-generation-async-storage-wrapper.js:42:24)
    at eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:196:97)
    at AsyncLocalStorage.run (node:async_hooks:341:14)
    at Object.wrap (webpack-internal:///(rsc)/./node_modules/next/dist/server/async-storage/request-async-storage-wrapper.js:82:24)
    at eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:195:75)
    at AsyncLocalStorage.run (node:async_hooks:341:14)
    at AppRouteRouteModule.execute (webpack-internal:///(rsc)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:192:56)
    at AppRouteRouteModule.handle (webpack-internal:///(rsc)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:315:41)
    at doRender (/Users/down/Desktop/dashboard/node_modules/next/dist/server/base-server.js:1091:56)
    at cacheEntry.responseCache.get.incrementalCache.incrementalCache (/Users/down/Desktop/dashboard/node_modules/next/dist/server/base-server.js:1300:34)
    at /Users/down/Desktop/dashboard/node_modules/next/dist/server/response-cache/index.js:99:42
    at ResponseCache.get (/Users/down/Desktop/dashboard/node_modules/next/dist/server/response-cache/index.js:149:11)
    at DevServer.renderToResponseWithComponentsImpl (/Users/down/Desktop/dashboard/node_modules/next/dist/server/base-server.js:1219:53)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async DevServer.renderPageComponent (/Users/down/Desktop/dashboard/node_modules/next/dist/server/base-server.js:1451:24)
    at async DevServer.renderToResponseImpl (/Users/down/Desktop/dashboard/node_modules/next/dist/server/base-server.js:1495:32)
    at async DevServer.pipeImpl (/Users/down/Desktop/dashboard/node_modules/next/dist/server/base-server.js:735:25)
    at async DevServer.handleCatchallRenderRequest (/Users/down/Desktop/dashboard/node_modules/next/dist/server/next-server.js:703:13)
    at async DevServer.handleRequestImpl (/Users/down/Desktop/dashboard/node_modules/next/dist/server/base-server.js:647:17)


import { NextResponse, type NextRequest } from "next/server"

export function GET(
  req: NextRequest,
  { params }: { params: { guild_id: string } }
) {
  return NextResponse.redirect(`/dashboard/${params.guild_id}`)
}
Answered by Giant panda
req.nextUrl.searchParams IIRC
View full answer

15 Replies

American ChinchillaOP
logging out params gave undefined

it was tested with this link http://localhost:3000/api/auth/server-auth?code=somecode&guild_id=12345&permissions=8
Giant panda
Those are search params.
Not path params.
American ChinchillaOP
oh, then how do i get those
Giant panda
req.nextUrl.searchParams IIRC
Answer
import { type NextRequest } from 'next/server'

export function GET(request: NextRequest) {
  const searchParams = request.nextUrl.searchParams
  const name = searchParams.get('name')
  return new Response(name ? `Hello ${name}!` : '`name` is not provided.')
}
American ChinchillaOP
import { notFound } from "next/navigation"
import { NextResponse, type NextRequest } from "next/server"

export function GET(req: NextRequest) {
  const guild_id = req.nextUrl.searchParams.get("guild_id")
  if (!guild_id) {
    notFound()
  }
  return NextResponse.redirect(`/dashboard/${guild_id}`)
}
this should be fine
@American Chinchilla i have to use absolute urls but yeah
I think if you use redirect() there you don’t need absolute url
@joulev I think if you use redirect() there you don’t need absolute url
American ChinchillaOP
yeah seems to be working thanks