Latihan toko buku. Book dapat diberikan satu Genre Mencoba menetapkan buku baru dengan genreId yang disediakan di DTO.

BukuRepositori

    @EntityRepository(Book)
    export class BookRepository extends Repository<Book> {
      constructor(
        private genreService: GenresService,
      ) {
        super();
      }

      async createBook(createBookDto: CreateBookDto): Promise<Book> {
        const genre = await this.genreService.getOne(createBookDto.genreId);
        const newBook = this.create({
          ...createBookDto,
          genre,
        });
        return newBook.save();
      }
    }

Layanan Genre

    @Injectable()
    export class GenresService {

      constructor(
        @InjectRepository(GenreRepository) private readonly genreRepository: GenreRepository,
      ) {}

      async getOne(id: number): Promise<Genre> {
        return this.getById(id);
      }

      private async getById(id: number): Promise<Genre> {
        const found = await this.genreRepository.findOne(id);

        if (!found) {
          throw new NotFoundException(`Genre with id ${id} not found.`);
        }

        return found;
      }
    }

BookRepository dan GenreRepository diimpor bersama oleh BookstoreModule, seperti:

    imports: [
        TypeOrmModule.forFeature([
          GenreRepository,
          BookRepository,
          AuthorRepository,
        ]),
        // ...etc
    },

NestJS mengeluarkan kesalahan berikut:

[ExceptionsHandler] this.genreService.getOne is not a function +1045981ms
TypeError: this.genreService.getOne is not a function
    at BookRepository.createBook (/Users/artur/Code/Sandbox/books-nest/dist/bookstore/books/book.repository.js:21:47)
    at BooksService.createBook (/Users/artur/Code/Sandbox/books-nest/dist/bookstore/books/books.service.js:29:36)
    at BooksController.create (/Users/artur/Code/Sandbox/books-nest/dist/bookstore/books/books.controller.js:31:33)

Mencoba menyuntikkan GenreRepository into theBookRepository`

constructor(
    @InjectRepository(GenreRepository) private genreRepository: GenreRepository,
){
    super();
}

Dan .findOne() dari sana, kesalahannya adalah:

No metadata for "2" was found. +26884ms
EntityMetadataNotFound: No metadata for "2" was found.

(2 menjadi id untuk genreId)

Tidak yakin apakah pendekatan saya untuk menabung sudah benar. Mungkin ide untuk menemukan genre dari dalam BookRepository salah dan harus diselesaikan dengan cara yang berbeda. Jika demikian, bagaimana?

0
user776686 10 Desember 2019, 20:04

1 menjawab

Jawaban Terbaik

Bagi saya metode createBook harus berada di dalam BookService, BookService perlu menyuntikkan GenreService dan kemudian Anda harus memanggil genreService.getOne(createBookDto.genreId) dari dalam BookService.< br/> Lebih masuk akal untuk membiarkan layanan menangani logika bisnis / orkestrasi aliran data imo.

Saya sarankan untuk mencobanya dengan kode berikut:

Layanan Buku

@Injectable()
    export class BookService {

      constructor(
        @InjectRepository(BookRepository) private readonly bookRepository: BookRepository, 
private genreService: GenreService // <= here you go, you inject the genreService into the bookService to take advantage of its methods
      ) {}

      async getOne(id: number): Promise<Genre> {
        return this.getById(id);
      }

      async createBook(createBookDto: CreateBookDto): Promise<Book> {
        const genre = await this.genreService.getOne(createBookDto.genreId);
        const newBook = this.create({
          ...createBookDto,
          genre,
        });
        return newBook.save();
      }

      private async getById(id: number): Promise<Genre> {
        const found = await this.bookRepository.findOne(id);

        if (!found) {
          throw new NotFoundException(`Book with id ${id} not found.`);
        }

        return found;
      }
    }

BukuRepositori

    @EntityRepository(Book)
    export class BookRepository extends Repository<Book> {}

Layanan Genre

    @Injectable()
    export class GenresService {

      constructor(
        @InjectRepository(GenreRepository) private readonly genreRepository: GenreRepository,
      ) {}

      async getOne(id: number): Promise<Genre> {
        return this.getById(id);
      }

      private async getById(id: number): Promise<Genre> {
        const found = await this.genreRepository.findOne(id);

        if (!found) {
          throw new NotFoundException(`Genre with id ${id} not found.`);
        }

        return found;
      }
    }

GenreRepositori

    @EntityRepository(Genre)
    export class GenreRepository extends Repository<Genre> {}

Beri tahu saya jika itu membantu :)

1
A. Maitre 10 Desember 2019, 19:54