javascriptnode.jstypescriptnestjstypeorm

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


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;
    }
}

Solution

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

    Here's what your module should probably look like.

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