const fetchPost = async (postId: string) => {
const res = await fetch(`/api/editpost/${postId}`)
const post = await res.json()
}
I tried to get the postId in API route like below, but it is not correct:
我尝试获取API路由中的postID,如下所示,但不正确:
export const GET = async (req: NextRequest) => {
const postId = req.nextUrl.searchParams.get('postId')
console.log(postId)
}
Any help would be greatly appreciated.
任何帮助都将不胜感激。
更多回答
优秀答案推荐
It should be done like below:
具体操作如下:
import { prisma } from '@/lib/prisma'
import { NextRequest, NextResponse } from 'next/server'
export async function GET(
request: NextRequest,
{ params }: { params: { postId: string } }
) {
const postId = params.postId
console.log('postId' + postId)
const post = await prisma.post.findUnique({
where: {
id: postId,
},
})
console.log('post: ' + post)
return NextResponse.json(post)
}
更多回答
我是一名优秀的程序员,十分优秀!