Next.js Discord

Discord Forum

params is an empty object

Answered
Sage Thrasher posted this in #help-forum
Open in Discord
Sage ThrasherOP
Logs:
{}
Server   {}
GET /servers/guild_add?code=45DDtkqa08lE2wBZfHyYIzzE5y&state=%252F&guild_id=1132739446215815248&permissions=1 307 in 664ms

As you can see by the URL, the params are there

Code:
import { redirect } from 'next/navigation';

export default async function Page({ params }: { params: { guild_id: string, state: string; } }) {
    console.log(params);

    if (params.guild_id) {
        return redirect(`/servers/${params.guild_id}/edit`);
    } else {
        return redirect('/');
    }
}
Answered by James4u
export default function Page({
  params,
  searchParams,
}: {
  params: { slug: string }
  searchParams: { [key: string]: string | string[] | undefined }
}) {
  if (params.guild_id) {
      return redirect(`/servers/${searchParams.guild_id}/edit`);
  } else {
      return redirect('/');
  }
}
View full answer

17 Replies

@Sage Thrasher you are trying to get search param right? params.guild_id is slug in dynamic routing
https://nextjs.org/docs/app/api-reference/file-conventions/page#searchparams-optional
check out this link
/servers/guild_add?code=45DDtkqa08lE2wBZfHyYIzzE5y&state=%252F&guild_id=1132739446215815248&permissions=1
this must be the link you are hitting right?
you should get guild_id from searchParams
@James4u this must be the link you are hitting right? you should get `guild_id` from searchParams
Sage ThrasherOP
im getting nothing but an empty object
i tried removing state to see if that was causing it
shwo me your code
Sage ThrasherOP
@Sage Thrasher use searchParam
did you check the link I shared?
Sage ThrasherOP
i said i was checking it but you replied saying i should receive the guild_id, i didnt know if you meant with my current code or post implementing searchparams
export default function Page({
  params,
  searchParams,
}: {
  params: { slug: string }
  searchParams: { [key: string]: string | string[] | undefined }
}) {
  if (params.guild_id) {
      return redirect(`/servers/${searchParams.guild_id}/edit`);
  } else {
      return redirect('/');
  }
}
Answer
try this out
@James4u I only said you should use searchParams instead of params
Sage ThrasherOP
works, thanks!
good to hear that!