gpt4 book ai didi

How to Properly Utilize req.params in Next.js API Route via App Router(如何通过应用路由器正确使用Next.js API Routing中的req.pars)

转载 作者:bug小助手 更新时间:2023-10-25 17:08:27 25 4
gpt4 key购买 nike



I'm working with Next.js and using Express to fetch data from the database. Although I can successfully trigger a GET request via Postman using Express, I'm unable to do so through the API folder in Next.js. I have attempted to use searchParams as suggested by the Next.js documentation, but this method was unsuccessful. I then tried using params following a discussion I found here, but this also did not solve the issue.

我正在使用Next.js并使用Express从数据库中获取数据。虽然我可以通过Postman使用Express成功触发GET请求,但我无法通过Next.js中的API文件夹这样做。我曾尝试使用Next.js文档中建议的searchParams,但这种方法不成功。然后,我在这里找到一个讨论,然后尝试使用参数,但这也没有解决问题。


Here is the URL structure I am using: http://localhost:5001/api/get-update-history/6

下面是我使用的URL结构:http://localhost:5001/api/get-update-history/6


And here's the relevant portion of my code:

下面是我的代码的相关部分:


import { NextResponse } from "next/server";
import prisma from '@/app/config/database';

export async function GET(request, { params }) {
const { toId } = params
try {

const driverUpdates = await prisma.UpdateHistory.findMany({
where: { toId: Number(toId) },
select: {
updatedAt: true,
afterValues: true,
beforeValues: true,
updatedByTo: {
select: {
name: true
},
},
updatedByUser: {
select: {
name: true,
}
}
}
});
if (driverUpdates && driverUpdates.length > 0) {
const response = driverUpdates.map(update => ({
updatedAt: update.updatedAt,
beforeValues: JSON.parse(update?.beforeValues),
afterValues: JSON.parse(update?.afterValues),
updateByUserName: update?.updatedByUser?.name,
updateByToName: update?.updatedByTo?.name

}));
return new NextResponse([JSON.stringify(response)]);
} else {
return NextResponse(`No updates for this driver`);
}

} catch (e) {
return NextResponse.error(`Error getting updates history: ${e}`);
}

}

}


Could someone advise me on how to modify my code to make the GET request work through the API folder in Next.js?

有人能建议我如何修改我的代码,使GET请求通过Next.js中的API文件夹工作吗?


更多回答
优秀答案推荐
更多回答

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