我正在使用Next.js和Prisma构建博客应用程序。我有一个页面,我显示一个职位的基础上,它的ID,我想增加的意见计数每次获取此页面。但是,视图计数根本不会改变。
下面是相关代码:
page.tsx:
timport BackButton from "@/components/BackButton";
import Badge from "@/components/Badge";
import DeleteButton from "@/components/DeleteButton";
import Like from "@/components/Like";
import Views from "@/components/Views";
import { Tag } from "@prisma/client";
import { Pencil } from "lucide-react";
import Link from "next/link";
interface Props {
params: {
id: string;
};
}
interface Post {
id: string;
title: string;
content: string;
tag: Tag;
views: number;
likes: number;
}
const getPost = async ({ params }: Props): Promise<Post> => {
const res = await fetch(`http://localhost:3000/api/posts/${params.id}`, {
cache: "no-store",
});
return res.json();
};
const DetailPage = async ({ params }: Props) => {
const post = await getPost({ params });
return (
<div className="flex flex-col gap-4 max-w-lg">
<BackButton href="/posts" />
<div className="flex justify-between gap-1 items-baseline max-w-sm">
<h1 className="text-3xl font-bold">{post.title}</h1>
{post.tag && <Badge tag={post.tag.name} />}
</div>
<div className="flex justify-start gap-4 items-baseline max-w-sm">
<div className="flex items-center gap-4">
<Link href={`/posts/edit/${post.id}`} className="btn btn-warning">
<Pencil /> Edit
</Link>
<DeleteButton params={params} />
</div>
<Views views={post.views} />
<Like likes={post.likes} />
</div>
<p className="max-w-md">{post.content}</p>
</div>
);
};
export default DetailPage;
route.tsx:
import prisma from "@/prisma/client";
import { NextRequest, NextResponse } from "next/server";
interface Props {
params: {
id: string;
};
}
export async function GET(req: NextRequest, { params }: Props) {
try {
const post = await prisma.post.update({
where: {
id: params.id,
},
data: {
views: {
increment: 1,
},
},
select: {
id: true,
title: true,
content: true,
tag: true,
views: true,
likes: true,
},
});
return NextResponse.json(post, { status: 200 });
} catch (error) {
return NextResponse.json(
{ message: "Could not fecth the post" },
{ status: 500 }
);
}
}
views.tsx:
import { Eye } from "lucide-react";
interface Props {
views: number;
}
const Views = ({ views }: Props) => {
console.log("****Views****: ", views);
return (
<div className="flex gap-1 items-center">
<Eye size={23} />
<span className="font-bold text-sm">{views}</span>
</div>
);
};
export default Views;
prisma.schema.ts:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
model Tag {
id String @id @default(cuid()) @map("_id")
name String
post Post[]
}
model Post {
id String @id @default(cuid()) @map("_id")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String
content String
views Int @default(0)
likes Int @default(0)
tag Tag? @relation(fields: [tagId], references: [id])
tagId String
}
- 我试过在增加视图数之前获取帖子,在增加视图数之后获取帖子,甚至在一个查询中使用update方法和select来获取和增加。然而,这些解决方案都没有奏效。
- 我不会在每个请求上都创建一个新的Prisma客户端示例,我的数据库连接是稳定的。当我尝试更新视图计数时,没有抛出错误。
有谁知道为什么视图计数可能不会更新,我如何解决这个问题?
1条答案
按热度按时间1yjd4xko1#
我用视图功能解决了这个问题。出现这个问题是因为我在prisma.schema.ts文件中定义视图字段之前已经创建了一些帖子。因此,这些员额在数据库中没有视图字段。为了解决这个问题,我使用Prisma Studio手动为每个帖子分配随机浏览次数。在那之后,所有帖子的浏览量都正常工作。