#ORM returning different types with same method

3 messages · Page 1 of 1 (latest)

cerulean ether
#

I'm confused.

I'm building an API that uses prisma as ORM, as per the documentation (https://docs.nestjs.com/recipes/prisma)

Now, If I use this service in a controller, everything works as expected. But if I use the very same method in a custom service, and returns the result in my controller, the returned type is not the same, and therefore, I get type errors

  // my service
  
  /**
   * Find one user
   */
  async findOne(userWhereUniqueInput: Prisma.UserWhereUniqueInput) {
    // Throw an error if the user doesn't exist
    return this.prisma.user.findUniqueOrThrow({
      where: userWhereUniqueInput,
    });
  }


  // [...]
  // my controller

  /**
   * Find one user
   */
  @Get()
  async findOne(userWhereUniqueInput: Prisma.UserWhereUniqueInput) {
    const user2 = this.prismaService.user.findUnique({
      where: userWhereUniqueInput,
    });

    const user2company = user2.company() // works just fine

    const user = await this.usersService.findOne(userWhereUniqueInput);
    const userCompany = user.company(); //raises an error: "Property 'company' does not exist on type..."
  }


Did I miss something ? In both cases, I use the very same method provided by Prisma

scenic stirrup
#

Remove async from findOne method definition on the service.

cerulean ether
#

Thanks a lot ! Does it mean that service methods should never use async ?