TypeError: Repository method is not a function (NestJS / TypeORM)

Viewed 10343

I'm working with NestJS and TypeORM. When trying to call the createMailLogEntry method of the repository, I'm getting the following error: TypeError: this.mailLogEntryRepository.createMailLogEntry is not a function

I can't figure out what's going wrong.

mailing.service.ts

@Injectable()
export class MailingService {

    constructor(@InjectRepository(MailLogEntryRepository) private mailLogEntryRepository: MailLogEntryRepository) { }

        // ...

        if(transferResult) {
            await this.mailLogEntryRepository.createMailLogEntry({
                creationDate: new Date(Date.now()),
                from: mailContent.from,
                to: mailContent.to,
                subject: mailContent.subject,
                text: mailContent.text,
                html: mailContent.html,
                cc: mailContent.cc,
                bcc: mailContent.bcc,
            });
        }
    }
}

mail-log-entry.repository.ts

@EntityRepository(MailLogEntry)
export class MailLogEntryRepository extends Repository<MailLogEntry> {

    async createMailLogEntry(mailLogEntryDto: MailLogEntryDto): Promise<MailLogEntry> {
        const mailLogEntry = MailLogEntryRepository.createMailLogEntryFromDto(mailLogEntryDto);
        return await mailLogEntry.save();
    }

    private static createMailLogEntryFromDto(mailLogEntryDto: MailLogEntryDto): MailLogEntry {
        const mailLogEntry = new MailLogEntry();
        mailLogEntry.from = mailLogEntryDto.from;
        mailLogEntry.to = mailLogEntryDto.to;
        mailLogEntry.subject = mailLogEntryDto.subject;
        mailLogEntry.text = mailLogEntryDto.text;
        mailLogEntry.html = mailLogEntryDto.html;
        mailLogEntry.cc = mailLogEntryDto.cc;
        mailLogEntry.bcc = mailLogEntryDto.bcc;

        return mailLogEntry;
    }
}
7 Answers

I came across this problem as well. Make sure you're importing the repository into the module.

Here's what your module should look probably like.

@Module({
  imports: [TypeOrmModule.forFeature([TeamRepository])],
  providers: [TeamService],
  controllers: [TeamController]
})
export class TeamModule {}

I had the same problem, I'll tell you what got me out of it if it helps someone else!

If your repository is handmade (not generated by an ORM) then you can't use the Repository injection :

Inject repository Image 1

Instead just use the repository as you would for a service (just remove the @InjectRepository(People)):

Inject repository image 2

I hope this will help!Happy coding !

PS : Personnes is my entity and the helper has nothing to do with the issue

Maybe this can help too, it's related with @Simple Dev answer about handmade repository. For example if you create an Entity, Service and Repositoy classes with different names probably when you try to call a repository member function from service will be undefined. Ex:

@Entity
export class Person { ... }

@Injectable()
export class OtherPersonService { ... }  // Person !== OtherPerson

@EntityRepository(Person)
export class OtherPersonRepository extends Repository<Person> { ... } // Same problem

I came across this problem as well, after rename my service and repository the error is gone. For the previous example the solution would be: Person, PersonService and PersonRepository.

For me, it was that I've imported the entity and the custom repository for that entity together.

@Module({
  imports: [TypeOrmModule.forFeature([
  TeamRepository,
  Team   // either the entity or the custom repository should be imported.
  ])],
  providers: [TeamService],
  controllers: [TeamController]
})
export class TeamModule {}

I know this is an old question but I'll leave this here hoping it might help someone. You can eliminate having the repository file and use @InjectRepository on the constructor of your service. Then you can access all the repository methods on your service. Make sure to include your entity on TypeOrmModule.forFeature on the imports of your module.

Here Product is my entity and this is my service file (ProductService) which I call from the controller. Happy coding!

@Injectable()
export class ProductService {
 private logger = new Logger('ProductService');
 constructor(
  @InjectRepository(Product)
  private productRepository: Repository<Product>
 ) { }

 async getAllAsync(): Promise<Product[]> {
  return await this.productRepository.find();
 }

 async getCountAsync(): Promise<number> {
  return await this.productRepository.count();
 }
}

@MFuat, Yes, finally fixed it this morning, mine was also related to unit testing the user.service.ts file. Turns out I wasn't mocking the register method in my test file. so once I mocked it,the test passed.

const mockUserRepository = {
  register: jest.fn().mockResolvedValue('sampleResolvedValue')
};

I came across this issue before. and it was saying that the function is Undefined. I tried so many solutions but with no luck and then I just changed the repo name from payment to payments and it worked with me. so try to change the repo name

Related