강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của workstation199611109
workstation199611109

câu hỏi đã được viết

Tìm hiểu NestJS bằng cách làm theo

Truy xuất các bài đăng cụ thể bằng ID

findOne(id) 에서 에러가 발생한 경우 해결법

Viết

·

3.5K

23

# Info


강의 업로드 연도(2021) 와 수강 연도(2022) 사이에 TypeORM 의 버전이 달라서, Repository.findOne() 메서드의 구성이 달라진 것 같다고 생각합니다.

강의 대로 코드를 작성하면 후술할 에러가  발생하는데, 해당 부분을 해결하고  나서, 다른 수강생 분들 도 이런 문제를 겪을까 생각되어서 따로 글로 남기게 되었습니다.


## 문제 세 줄 요약


1. fineOne( id) 를 하면 에러가 발생

2. 관련 레퍼런스가 없어서 TypeORM docs 확인

3. fineOneBy({id}) 로 에러 해결 (2022-03-30)


자세한 내용은 ### 해결방법, ### 참고문서, ### 초기질문 참고해주세요.

깃 : unchaptered/22-03-nestjs-board: Nest.JS (github.com)


### 해결방법

 

2022년 3월 30일 기준으로,

this.boardsRepository.findOne( id );

위와 같이 입력을 했는데 에러가 발생했다면, 해당 부분을 다음의 코드로 교체해서 해결할 수 있습니다.

this.boardsRepository.fineOneBy({ id });



### 참고문서



아래 페이지에서 Ctrl + F 로 fineOne 혹은 fineOneBy 를 검색해서 확인하시면 됩니다.

TypeORM - Amazing ORM for TypeScript and JavaScript (ES7, ES6, ES5). Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.



### 초기 질문



boards.service.ts 의 getBoardById() 에서

this.boardsRepository.fineOne(id); 를 하면 id 에 붉은 경고가 다음과 같이 발생하고 있습니다.

혹시 해당 부분이 왜 문제가 되는지 알 수 있을까요?

정크 데이터까지만 푸쉬 해놓았지만,

node_module 버전 문제일까 싶어서 깃 허브 링크도 최하단에 올려놓겠습니다.

 

위의 에러가 발생하는 해당 코드입니다.

    async getBoardById(id: number): Promise<Board> {

        const found = await this.boardsRepository.findOne(id);
        if (!found) throw new NotFoundException(`Can't find Board by ${id}`);

        return found;
       
    }

엔티티

import { BaseEntity, Column, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
import { BoardStatus } from "./board-status.enum";

export class Board extends BaseEntity {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    title: string;

    @Column()
    description: string;

    @Column()
    status: BoardStatus;
}

baords.repository.ts

import { EntityRepository, Repository } from "typeorm";
import { Board } from "./entity/board.entity";

@EntityRepository(Board)
export class BoardsRepository extends Repository<Board> {

}

 

깃허브 : unchaptered/22-03-nestjs-board: Nest.JS (github.com)

fineOnepostgresqlfineOneBytypeormTypeORMjwtNestJS

Câu trả lời 6

3

findOneBy({id}) 같네요.. fineOne이 아니라..

1

또는 .findOne({ where: {id}})

0

감사합니다!!!!!!

0

와우 감사합니다. 

0

정말 감사합니다!!! 

0

덕분에 쉽게 해결했습니다.  감사합니다!

unchaptered님의 프로필 이미지
unchaptered
Người đặt câu hỏi

쉽게 해결하셨다니 다행입니다..! :_)

Hình ảnh hồ sơ của workstation199611109
workstation199611109

câu hỏi đã được viết

Đặt câu hỏi