server searchParams
Answered
Jack Russell Terrier posted this in #help-forum
Jack Russell TerrierOP
export async function GET(request: Request, { searchParams }: { searchParams?: { id?: string; }; }) {
console.log(`searchParams: ${searchParams}`)
const db = await openDB()
const event = await db.get('SELECT * FROM event WHERE id = ?', [searchParams?.id])
return Response.json(event)
}Why is searchParams undefined? (My request: http://localhost:3000/api/anon/event/get?id=1; File location: src/app/api/anon/event/get/route.ts)
Answered by joulev
You don’t have
searchParams in that object, only params. To get the searchParams: https://nextjs.org/docs/app/building-your-application/routing/route-handlers#url-query-parameters6 Replies
You don’t have
searchParams in that object, only params. To get the searchParams: https://nextjs.org/docs/app/building-your-application/routing/route-handlers#url-query-parametersAnswer
Jack Russell TerrierOP
then the docs seem a little confusing to me. For me this implies that I get searchParams by default
export default function Page({
params,
searchParams,
}: {
params: { slug: string }
searchParams: { [key: string]: string | string[] | undefined }
}) {
return <h1>My Page</h1>
}
export default function Page({
params,
searchParams,
}: {
params: { slug: string }
searchParams: { [key: string]: string | string[] | undefined }
}) {
return <h1>My Page</h1>
}
@Jack Russell Terrier then the docs seem a little confusing to me. For me this implies that I get searchParams by default
export default function Page({
params,
searchParams,
}: {
params: { slug: string }
searchParams: { [key: string]: string | string[] | undefined }
}) {
return <h1>My Page</h1>
}
Page and route files have wildly different syntaxes
Your example is for a page, but the one in question is a route
Jack Russell TerrierOP
oh, thanks