gpt4 book ai didi

How to get the postId in NextJS 13(如何获取NextJS 13中的postID)

转载 作者:bug小助手 更新时间:2023-10-25 16:39:32 25 4
gpt4 key购买 nike



 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)
}

更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com