인프런 커뮤니티 질문&답변
Cast to ObjectId failed for value라는 에러 발생합니다!
작성
·
1.6K
1
수강생분들의 질문을 기다립니다!
- 에러에 해당하는 질문은 "에러가 발생한 상황에 대한 충분한 설명", "에러 메세지", "에러가 난 코드 스크린샷"을 함께 첨부해주세요.
- 언어에 해당하는 질문은 구글링 및 서치 후에 구체적으로 질문해주시면 좋습니다.
- 간단한 진로 및 방향성에 대한 질문은 메일로 보내주세요.
- 패키지 버전 관리은 실무 환경과 트랜드에 맞추어 강의를 업데이트 하고 있습니다. 강의를 그대로 따라갔는데 에러가 발생한다면 패키지 버전을 강의에서 사용하는 버전과 동일하게 맞춰주세요!
- 강의 노트, QA 목록, 공지 사항을 먼저 확인해주세요.
- 논리적이고 구체적인 질문은 학습 효과를 올립니다 :)
- 에러에 해당하는 질문은 "에러가 발생한 상황에 대한 충분한 설명", "에러 메세지", "에러가 난 코드 스크린샷"을 함께 첨부해주세요.
- 언어에 해당하는 질문은 구글링 및 서치 후에 구체적으로 질문해주시면 좋습니다.
- 간단한 진로 및 방향성에 대한 질문은 메일로 보내주세요.
- 패키지 버전 관리은 실무 환경과 트랜드에 맞추어 강의를 업데이트 하고 있습니다. 강의를 그대로 따라갔는데 에러가 발생한다면 패키지 버전을 강의에서 사용하는 버전과 동일하게 맞춰주세요!
- 강의 노트, QA 목록, 공지 사항을 먼저 확인해주세요.
- 논리적이고 구체적인 질문은 학습 효과를 올립니다 :)
comments.service
import { CatsRepository } from 'src/cats/cats.repository';
import { Comments } from './../comments.schema';
import { CommentsCreateDto } from './../dto/comments.create.dto';
import { BadRequestException, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@Injectable()
export class CommentsService {
constructor(
@InjectModel(Comments.name)
private readonly commentsModel: Model<Comments>,
private readonly catsRepository: CatsRepository,
) {}
async getAllComments() {
try {
const comments = await this.commentsModel.find();
return comments;
} catch (error) {
throw new BadRequestException(error.message);
}
}
async createComment(id: string, commentData: CommentsCreateDto) {
try {
const targetCat = await this.catsRepository.findCatByIdWithOutPsw(id);
const { contents, author } = commentData;
const validatedAuthor = await this.catsRepository.findCatByIdWithOutPsw(
author,
);
const newComment = new this.commentsModel({
author: validatedAuthor._id,
contents,
info: targetCat._id,
});
return await newComment.save();
} catch (error) {
throw new BadRequestException(error.message);
}
}
async plusLike(id: string) {
try {
const comment = await this.commentsModel.findById(id);
comment.likeCount += 1;
return await comment.save();
} catch (error) {}
}
}
{
"success": false,
"timestamp": "2022-02-22T10:53:22.484Z",
"statusCode": 400,
"message": "Cast to ObjectId failed for value \"66213fe65d99f4623dae55787\" (type string) at path \"_id\" for model \"Cat\"",
"error": "Bad Request"
}
jwt.strategy에서는 payload에 있는 sub.id 역시 스트링으로 잘 넘어갔는데 왜 쿼리 id는 오류가나는지 궁금합니다
https://github.com/minjamie/NestJS-A-to-Z 깃허브 주소 남깁니다!





.png?w=112)