인프런 커뮤니티 질문&답변

lgh3806님의 프로필 이미지
lgh3806

작성한 질문수

따라하며 배우는 NestJS

게시물 상태 업데이트하기

update를 repository로 빼서 작성했는데 오류가 나옵니다

작성

·

461

0

boards.service.ts 코드입니다.

import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { BoardStatus } from './board-status.enum';
import { Board } from './board.entity';
import { BoardRepository } from './board.repository';
import { CreateBoardDto } from './dto/create-board.dto';

@Injectable()
export class BoardsService {
  constructor(
    @InjectRepository(Board)
    private boardRepository: BoardRepository,
  ) {}

  createBoard(createBoardDto: CreateBoardDto): Promise<Board> {
    return this.boardRepository.createBoard(createBoardDto);
  }

  async getBoardById(id: number): Promise<Board> {
    console.log(id, 'ididid');

    return this.boardRepository.getBoardById(id);
  }

  async deleteBoard(id: number): Promise<void> {
    const result = await this.boardRepository.delete(id);
    if (result.affected == 0) {
      throw new NotFoundException(`Can't find Board with id ${id}`);
    }
  }

  async updateBoardStatus(id: number, status: BoardStatus): Promise<Board> {
    const board = await this.getBoardById(id);
    board.status = status;
    await this.boardRepository.save(board);

    return board;
  }
}

board.repository.ts 코드입니다

import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { BoardStatus } from './board-status.enum';
import { Board } from './board.entity';
import { BoardRepository } from './board.repository';
import { CreateBoardDto } from './dto/create-board.dto';

@Injectable()
export class BoardsService {
  constructor(
    @InjectRepository(Board)
    private boardRepository: BoardRepository,
  ) {}

  createBoard(createBoardDto: CreateBoardDto): Promise<Board> {
    return this.boardRepository.createBoard(createBoardDto);
  }

  async getBoardById(id: number): Promise<Board> {
    console.log(id, 'ididid');

    return this.boardRepository.getBoardById(id);
  }

  async deleteBoard(id: number): Promise<void> {
    const result = await this.boardRepository.delete(id);
    if (result.affected == 0) {
      throw new NotFoundException(`Can't find Board with id ${id}`);
    }
  }

  async updateBoardStatus(id: number, status: BoardStatus): Promise<Board> {
    const board = await this.getBoardById(id);
    board.status = status;
    await this.boardRepository.save(board);

    return board;
  }
}

[Nest] 12296 - 2023. 01. 26. 오후 4:25:43 ERROR [ExceptionsHandler] this.boardRepository.getBoardById is not a function

TypeError: this.boardRepository.getBoardById is not a function

at BoardsService.getBoardById (C:\Users\lgh38\Desktop\GH\nest-board-app\src\boards\boards.service.ts:22:33)

at BoardsService.updateBoardStatus (C:\Users\lgh38\Desktop\GH\nest-board-app\src\boards\boards.service.ts:33:30)

at BoardsController.updateBoardStatus (C:\Users\lgh38\Desktop\GH\nest-board-app\src\boards\boards.controller.ts:44:31)

오류 내용은 위와 같습니다. 자세히 보면 this.boardRepository.getBoardById is not a function이라고 나오는데 제가 생각 했을 땐 처음 service의 updateBoardStatus 함수에서 getBoardById 함수로 넘겨주고 또 getBoardById 함수가 repository 안에 있는 getBoardById 함수에게 값을 넘겨서 두 번 리턴 받아서 해결 될 줄 알았는데 에러가 나오더라구요..

오히려 분리를 안 시켰을 땐 오류가 안 나옵니다 무슨 이유일까요..?

답변

답변을 기다리고 있는 질문이에요
첫번째 답변을 남겨보세요!
lgh3806님의 프로필 이미지
lgh3806

작성한 질문수

질문하기