nestjs project typeorm 3.x version repository.find issue

23 Views Asked by At

I'm working on a project using nestjs, typeorm. I made a simple userRepository and called the find function, but it keeps returning empty data

The related versions of package.json are as follows.

"@nestjs/core": "^10.0.0", "@nestjs/typeorm": "^10.0.1", "typeorm": "^0.3.17"

The userrepository code is as follows.

// // user.repository.ts
import { Repository } from 'typeorm';
import { User } from './user.entity';
import { InjectRepository } from '@nestjs/typeorm';
import {NotFoundException} from "@nestjs/common";

export class UserRepository extends Repository<User> {
constructor(
@InjectRepository(User) private userRepository: Repository<User>,
) {
 super(
   userRepository.target,
   userRepository.manager,
   userRepository.queryRunner,
 );
}

async getLinkUsers(ci: string): Promise<User[]> {

return this.userRepository.find({
  select: {
   email : true,
   ci : true
  },
  where : {ci : ci}
  });
 }
}

The following query is executed when the corresponding getLinkUsers() function is called

query: SELECT `User`.`ci` AS `User_ci`, `User`.`email` AS `User_email`, `User`.`id` AS `User_id` FROM `user` `User` WHERE (`User`.`ci` = ?) -- PARAMETERS: [{"ci":"test-ci"}]

The following query is executed by converting part where: {ci: ci} to code such as where: {ci: 'test-ci'}.

query: SELECT `User`.`ci` AS `User_ci`, `User`.`email` AS `User_email`, `User`.`id` AS `User_id` FROM `user` `User` WHERE (`User`.`ci` = ?) -- PARAMETERS: ["test-ci"]`

It is normal only when the code is executed as shown below, but as in the above case, the data seems to be put in the ci value in the form of json. Someone help me with the project is not progressing.

async getLinkUsers(ci: string): Promise<User[]> {
        return this.createQueryBuilder('user')
            .select(['user.email', 'user.ci'])
            .where('user.ci = :ci', { ci })
            .getMany();
    }

The same issue is occurring even if it is executed with the query builder.

0

There are 0 best solutions below