Next15 Api route in app-router
Answered
English Angora posted this in #help-forum
English AngoraOP
This function works in next14
but cause this error once i updated next to next15
how we should use api router in next15? I can't find any example on the docs
// src/app/api/example/[id]/route.ts
interface Props {
params: { id: string };
}
export async function POST(request: NextRequest, { params }: Props) {but cause this error once i updated next to next15
Type error: Route "src/app/api/example/[id]/route.ts" has an invalid "POST" export:
Type "Props" is not a valid type for the function's second argument.
how we should use api router in next15? I can't find any example on the docs
Answered by James4u
params are now async! Next.js v15 changes
https://nextjs.org/blog/next-15#async-request-apis-breaking-change
@English Angora
https://nextjs.org/blog/next-15#async-request-apis-breaking-change
@English Angora
6 Replies
params are now async! Next.js v15 changes
https://nextjs.org/blog/next-15#async-request-apis-breaking-change
@English Angora
https://nextjs.org/blog/next-15#async-request-apis-breaking-change
@English Angora
Answer
English AngoraOP
But how do you define the type for params and how do you retrieve them? There’s nothing in import { params } from 'next/navigation'
There is no example with params in the docs
There is no example with params in the docs
@James4u
ok, codemon works, but...
now i have got eslint warning - Is there a way to define the type for params, to avoid using any ?
export async function GET(request: NextRequest, props: any) {
const params = await props.params;now i have got eslint warning - Is there a way to define the type for params, to avoid using any ?
ok, got it, Solved
If anyone is looking for a solution to this problem, here’s how to solve it.
interface Props {
params: Promise<{ id: string }>;
}
export async function GET(request: NextRequest, props: Props) {
const params = await props.params;