Ошибка типа: метод репозитория не является функцией (Nest JS / TypeORM) - PullRequest
0 голосов
/ 20 марта 2020

Я работаю с Nest JS и TypeORM. При попытке вызвать метод репозитория createMailLogEntry я получаю следующую ошибку: TypeError: this.mailLogEntryRepository.createMailLogEntry is not a function

Не могу понять, что происходит.

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

1 Ответ

0 голосов
/ 14 апреля 2020

Я тоже сталкивался с этой проблемой. Убедитесь, что вы импортируете репозиторий в модуль.

Вот как должен выглядеть ваш модуль.

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