'Dependency injection is undefined in exported service of Nestjs

I have CurrencyService that I want to use in another module. Here's what I did:

import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import { ScheduleModule } from '@nestjs/schedule';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CurrencyRepository } from './currency.repository';
import { CurrencyService } from './currency.service';

@Module({
  imports: [
    HttpModule,
    ScheduleModule.forRoot(),
    TypeOrmModule.forFeature([CurrencyRepository]),
  ],
  exports: [CurrencyService],
  providers: [CurrencyService],
})
export class CurrencyModule {}

In my CurrencyService, I have injected a repository and another service:

export class CurrencyService {
  constructor(
    private currencyRepository: CurrencyRepository,
    private httpService: HttpService,
  ) {}

  async getCurrency(base: string, target: string): Promise<Currency> {
    console.log(this.currencyRepository);
.....

My problem is that when I import the CurrencyModule, and inject the CurrencyService into the service of another module, currencyRepository and httpService are both undefined.

There is no error on startup, it's just that the dependencies are undefined which causes error on runtime.


I also tried something like this:

import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import { ScheduleModule } from '@nestjs/schedule';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CurrencyRepository } from './currency.repository';
import { CurrencyService } from './currency.service';

@Module({
  imports: [
    HttpModule,
    ScheduleModule.forRoot(),
    TypeOrmModule.forFeature([CurrencyRepository]),
  ],
  exports: [
    CurrencyService,
    TypeOrmModule.forFeature([CurrencyRepository]),
    HttpModule,
  ],
  providers: [CurrencyService],
})
export class CurrencyModule {}

I got the same undefined dependencies.



Solution 1:[1]

The @Injectable() decorator is missing from the CurrencyService. This decorator (or any decorator really) is what tells Typescript to emit the metadata of the controller that Nest is reading, so it is crucial to have it in place.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jay McDoniel