@mryuya110 (bizi ryu)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

自分が作成した投稿のみを一覧で表示させたい。

ユーザAが投稿したものを一覧で表示させたいです。
現在こんな感じに記入していますが、わからないです。
title,content,createdAtのみをUIに表示させたいです。

model Post {
  authorId  Int?
  content   String?
  id        Int     @id @default(autoincrement())
  published Boolean @default(false)
  title     String
  author    User?   @relation(fields: [authorId], references: [id])
  createdAt DateTime  @default(now())
  updatedAt DateTime @updatedAt
  TagsOnPosts      TagsOnPosts[]
}

model User {
  email String  @unique
  id    Int     @id @default(autoincrement())
  name  String?
  createdAt DateTime  @default(now())
  updatedAt DateTime @updatedAt
  posts Post[]
  password String 
  salt String
}

model Tag {
  id     Int     @id @default(autoincrement())
  name   String
  TagsOnPosts  TagsOnPosts[]
}

model TagsOnPosts {
  post        Post @relation(fields: [postId], references: [id])
  postId      Int
  tag         Tag  @relation(fields: [tagId], references: [id])
  tagId       Int

  @@id([postId, tagId])
}
user.ts
import prisma, { Post } from "../../lib/prisma";
import type { NextApiHandler } from 'next'

export type Posts = Pick<Post, "id" | "title" | "content" | "createdAt" | "authorId">[];

const getUserPost: NextApiHandler = async (req, res) => {
  try {
    const userPost = await prisma.post.findMany({
      where: {
        author: {
          title: true,
          content: true,
          id: true,
          createdAt: true,
          authorId: true,
        },
      },
    })
  res.status(200).json(userPost);
  } catch (error) {
    res.json({ error })
  }
};

export default getUserPost;
0 likes

No Answers yet.

Your answer might help someone💌