如何使用Prisma通过查询来过滤数据? 我正在尝试使用标题或使用Publication_date或部门动态过滤论文。或者只是一个查询。例如,这是我的URL,所以这意味着我必须在测试中过滤名称...

问题描述 投票:0回答:1

department

动态过滤纸。或者只是一个查询。例如,这是我的
URL
,所以这意味着我必须用
test
the the to开始to tos to。从
1999
2020
.
http://localhost:3000/api/filter-papers?title=test&yearStart=1999&yearEnd=2020&department=CAHS

这是我的api。

export async function GET(req: Request, res: NextResponse) { try { const { searchParams } = new URL(req.url) const searchTitle = searchParams.get('title') const yearStart = searchParams.get('year_start') const yearEnd = searchParams.get('year_end') const department = searchParams.get('department') const papers = await db.publish_Papers.findMany({ where: { OR: { title: { contains: searchTitle ?? "" }, publication_date: { gte: yearStart, lte: yearEnd }, department: department } } }) console.log(papers) return NextResponse.json(papers, { status: 200 }) } catch (error) { return NextResponse.json({ error: 'failed to load data' }, { status: 500 }) } }

这是我的架构。

model Publish_Papers { id String @id @default(uuid()) title String description String categories String publication_source String publication_date DateTime url String createdAt DateTime @default(now()) updatedAt DateTime @updatedAt authorId String Authors Authors @relation(fields: [authorId], references: [id]) }

因此,当我只运行时,没有任何过滤器。我得到了这个json
data.json

findMany
    

我检查了问题 - 您的问题是使用还是错。
不是在Prisma中的工作方式或工作。 另外,您的URL具有“年度”和“年末”,但您的代码寻找“ year_start”和“ year_end”。它的病例敏感。 尝试这种方法:

[ { "id": "4b20caba-65cd-4db6-b1e2-ecfe247b7dc4", "title": "publish test", "description": "http://localhost:3000/admin-resource/new", "categories": "CASE", "publication_source": "idk", "publication_date": "2016-03-10T01:37:21.562Z", "url": "http://localhost:3000/admin-resource/new", "createdAt": "2025-03-18T01:37:35.054Z", "updatedAt": "2025-03-18T01:37:35.054Z", "authorId": "d7b32e45-b25a-41b9-bc7b-178991409af0", }, ]

这种方式仅添加了实际存在于URL中的参数的过滤器。我建议您尝试实施这种方法,看看它是否有效。
    
javascript json next.js prisma
1个回答
0
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.