11월 마지막 주차 오답노트

Juno_Dev1·2025년 12월 3일

오늘은 이번 주에 제가 겪었던 실수들을 정리 해봤어서요.

❗️ 에러메시지

Argument of type '{ id: number; email: string; nickname: string | null; ... }'
is not assignable to parameter of type 'User'

문제점

 passport.deserializeUser(async function (id: number, done) {

  const user = await prisma.user.findUnique({ where: { id } });
  console.log(typeof user)
  done(null, user);

해결방법

내가 express.d.ts에 썻던 닉넴 타입과 Prisma model닉넴 타입이 안맞아서 문제 발생. express.d.ts User nickname 타입을 프리즈마 유저 모델 타입 맞춰줬어요


❗️에러 메시지

  Type '{ url: { ...... }.....}[]' is not assignable to type 'ImageCreateManyInput | ImageCreateManyInput[]'.
  
  Types of property 'url' are incompatible.

문제점:

처음에 전 분명히 하나의 게시글은 여러개의 사진을 가지고온다라는 생각을 했는데요? 제 실수는 여러 개의 사진들을 어떻게 배열화해서 가지고 오느냐 였습니다. 즉 저 문제는 객체 !== 문자열이니 객체부분을ㅇㅇ (덤으로 프리즈마 칼럼 실수도 있었음)

해결방법:

우선 프리즈마에 하나의 게시글에서 여러개 사진을 업로드 할수있다라는 가정을 둡니다

// prisma 모델
model Article{
	id Int
    ...
    images Image[] 
}
model Images{
	id 	Int
    url String
    ...
    articleId Int 
    article Article relataion(...) // ✅ 다대일 관계 
}
//articleService.ts
async createArticle(
	userId:Number,
    element: ArticleDTO
):Promise<ArticleDTO>{
	 const { title, content, ownerId, comments, images } = elements;

	const result = await prisma.article.create({
    	data:{
         	title: title??""
          	content: content ?? ""
        	comments:{
            	if (comments.length > 0 && comments){
                create: comments.map((c) => {
                	id :c.id,
                    content: c.content
                	})
                }
            },
          images:{
          	if (images && images.length> 0){
            	create: images.map((img)=>({
                  url:img.url
                })) ✅ 객체를 배열화 해주는 과정
            }
          }
        },
          include:{
            comments: true,
            images: true
          }
    })
}

0개의 댓글