nestjs / courses.nestjs.com

Official NestJS Courses website https://courses.nestjs.com 🏡
https://courses.nestjs.com
MIT License
127 stars 12 forks source link

Course fix suggestion - 'Add PostgreSQL with TypeORM: Using repository to Access Database' #57

Closed Nagell closed 2 years ago

Nagell commented 2 years ago

Hi,

In the chapter 'Add PostgreSQL with TypeORM: Using repository to Access Database' after installing TypeORM in version 0.3 or above the method 'findOne' is no more correct and there is no suggestion which one should be installed. According to TypeORM changelog:

findOne() signature without parameters was dropped. If you need a single row from the db you can use a following syntax:

const [user] = await userRepository.find()

This change was made to prevent user confusion. See https://github.com/typeorm/typeorm/issues/2500 for details.

findOne(id) signature was dropped. Use following syntax instead:

const user = await userRepository.findOneBy({
    id: id // where id is your column name
})

I can imagine that making a new video every time when something changes, would be to much, but some notice in the code below could be helpful :)

kamilmysliwiec commented 2 years ago

We're currently working on re-recordings of the entire Postgres chapter and will update videos shortly :)

thxv3n0lvl commented 2 years ago

So first of all, apologies to the authors for posting this as the Postgresql bit will change as mentioned in the previous comment...

The error:

src/coffees/coffees.service.ts:20:56 - error TS2559: Type 'string' has no properties in common with type 'FindOneOptions<Coffee>'.

20     const coffee = await this.coffeeRepository.findOne(id);

I faced this today and wasn't a huge deal to fix, nevertheless I lost my focus on the course to find out the reason... not sure if this affects the rest of the samples from the course but this is the way I did it:

  1. changed the id parameters type from string to number from my coffee.controller.ts and coffee.service.ts
  2. In the coffee.service.ts::CoffeesService.findOne() I changed the use of findOne(id); to findOneBy({id}); The resulting function was
    async findOne(id: number) {
    const coffee = await this.coffeeRepository.findOneBy({id});
    ... snip snip ...
    }

    after that I just checked again the console and everything was working as expected again :) BTW, nice course I love it !!!