我有一个next.JS项目,我使用Prisma和Mongodb,其中有动态路由,基本上当我们有**[somefolder]/page时。tsx然后在浏览器中点击https://localhost/somefolder/new**,它应该转到一个空表单的页面,但它不工作,但是如果我转到https://localhost/somefolder/someId,我知道我可以创建类似**/[somefolder]/new/page的内容。tsx**,但我想要https://localhost/somefolder/new和**[somefolder]/page。tsx工作,我得到这个错误时,去https://localhost/somefolder/new**
x1c 0d1x的数据
我的页面.tsx在somefolder看起来像:
import { ProductForm } from "@/components/product-form";
import prismadb from "@/lib/prismadb";
const ProductPage = async ({ params }: { params: { productId: string } }) => {
const product = await prismadb.product.findUnique({
where: {
id: params.productId,
},
});
return (
<div className="w-full p-10 m-10">
<ProductForm initialData={product} />
</div>
);
};
export default ProductPage;
字符串
知道吗?
1条答案
按热度按时间gupuwyp21#
当访问
https://localhost/somefolder/<productId>
时,params.productId
将被分配<productId>
的值。在访问
https://localhost/somefolder/someId
的情况下,params.productId
的值为someId
,我假设它是Mongodb中某个项目的id,所以它工作得很好。当访问
https://localhost/somefolder/new
时,params.productId
的值为new
,这是一个无效的id,无法在数据库中查找,因此您会得到错误。要处理这个问题,一个解决方案是创建一个特殊的页面来处理特定的路径,位于
app/(dashboard)/(routes)/products/[productId]/new/page.tsx
。这样,每当new
在URL中时,页面将优先于动态路径。