Next.js Discord

Discord Forum

Get param from url

Unanswered
Minskin posted this in #help-forum
Open in Discord
MinskinOP
Hi, i need get code=xxx from url, but req.query dont work. When i use req in console log, i can see code but when i use req.query i get error

17 Replies

@Minskin Hi, i need get code=xxx from url, but req.query dont work. When i use req in console log, i can see code but when i use req.query i get error
European sprat
You've confused page router API routes with app router route handlers
If you're using the app directory, look at the route handler docs
If you intend to use pages API routes, look at the docs for that and move your code outside of app directory and into pages/api/
MinskinOP
And what is better ?
either follow the docs for [route handlers](https://nextjs.org/docs/app/building-your-application/routing/route-handlers) (and make it route.ts), or move your api to pages dir
@Minskin And what is better ?
it would be better to use app dir as its the latest version of nextjs (and i think has better support for things)
MinskinOP
Ok, thanks
also sidenote, your metadata is useless as it isn't a page.tsx (it should be route.ts)
@Minskin let me know when it works 🙂
@riský <@214246901262974977> let me know when it works 🙂
MinskinOP
it works
but i need redirect
@Minskin but i need redirect
Redirect directly from wherever you are fetching the response(using the route handler)
Just return a response from your Route Handler then you can check the response in your component (where you are fetching) and use redirect() there.
MinskinOP
You mean return new Response() ?
MinskinOP
and next, how to save data to session ?
MinskinOP
@riský how to save data to session ?