page.tsx -->
"use client"
import useSWR from "swr"
type TestType = {
userId: number,
id: number,
title: string,
completed: boolean
}
async function fetcher(key: string) {
return fetch(key).then((res) => res.json() as Promise<TestType | null>)
}
const Page = () => {
const { data } = useSWR(`/api/test/1`, fetcher, { refreshInterval: 1000 }) || null
console.log(data)
return (
<div>Test</div>
)
}
export default Page
api/test/[id]/route.ts -->
export async function GET({params}: {params: {id: string}}) {
try {
const res = await fetch(`https://jsonplaceholder.typicode.com/todos/${params.id}`)
const data = await res.json()
return Response.json(data)
} catch (error) {
return new Response("Internal Error", {status: 500})
}
}
I get an error when I use it this way. But when I replace {params.id} with '1' I don't get any errors. What is the reason?