node.jstypescriptnestjstypeormnode.js-typeorm

NestJS y TypeORM: built in Repository undefined


i did Inject Repository(User) but it did not work for me .

i want to call : this.users Repository.create

but give this error : Type Error: this.users Repository.create is not a function

......

i did Inject Repository(User) but it did not work for me .

i want to call : this.users Repository.create

but give this error : Type Error: this.users Repository.create is not a function

Service :

import { HttpException, HttpStatus, Inject, Injectable,forwardRef } from '@nestjs/common';
import { AuthenticationService } from 'src/authentication/authentication.service';
import { Repository } from 'typeorm';
import CreateUserDto from './dto/create-user.dto';
import { InjectRepository } from '@nestjs/typeorm';
import User from './entities/user.entity';
@Injectable()
export class UserService {
  
  constructor(
    @Inject(forwardRef(() => AuthenticationService))
    // @Inject(User)

    // private usersRepository: Repository<User>
    @InjectRepository(User) private usersRepository: Repository<User>,
    private readonly authenticationService: AuthenticationService,


  ) {}

  async getByEmail(email: string) {
    const user = await this.usersRepository.findOne({ email });
    if (user) {
      return user;
    }
    throw new HttpException('User with this email does not exist', HttpStatus.NOT_FOUND);
  }

  async getById(id: number) {
    const user = await this.usersRepository.findOne({ id });
    if (user) {
      return user;
    }
    throw new HttpException('User with this id does not exist', HttpStatus.NOT_FOUND);
  }

  async create(userData: CreateUserDto) {

    const newUser = await  this.usersRepository.create(userData);

     await this.usersRepository.save(newUser);
    return newUser;
  }
}

Module :

import { Module,forwardRef } from '@nestjs/common';
import { UserService } from './users.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import User from './entities/user.entity';
import { UsersController } from './users.controller';
import { AuthenticationService } from 'src/authentication/authentication.service';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtModule, JwtService } from '@nestjs/jwt';
 
@Module({
  imports: [TypeOrmModule.forFeature([User]),JwtModule.register({})],
  providers: [UserService,AuthenticationService,ConfigService],
  exports: [UserService,AuthenticationService,ConfigService],
  controllers:[UsersController]
})
export class UsersModule {}

Entity :

import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
 
@Entity('User')
class User {
  @PrimaryGeneratedColumn()
  public id?: number;
 
  @Column({ unique: true })
  public email: string;
 
  @Column()
  public name: string;
 
  @Column()
  public password: string;
}
 
export default User;

Solution

  • I found a solution by creating a custom repository and extend it with built in works fine.

    Here is link : https://clownhacker.tistory.com/250

    I think it happened because I did changes with my entity class, I hope it will help someone.