1

I have a NestJS App and I have a Class where the app generates multiple instances from. Inside that class I need to access a service method but I dont know how to inject a service into a plain class.

This is the service I want to use inside the class "Stream"

import { Injectable } from '@nestjs/common';
import * as technicalindicators from 'technicalindicators';
import { CandleIndex } from 'src/utils/CandleIndex';
import * as ccxt from 'ccxt';
@Injectable()
export class AnalysisService {
  async RSI(ohlcv: ccxt.OHLCV[], period: number) {
    const close = ohlcv.map((candle) => candle[CandleIndex.CLOSE]);
    const result = technicalindicators.rsi({ values: close, period: period });
    return result;
  }
}

import { AnalysisService } from './analysis.service';
import { Module } from '@nestjs/common';

@Module({
  imports: [],
  controllers: [],
  providers: [AnalysisService],
  exports: [AnalysisService],
})
export class AnalysisModule {}

Here is the class I want to get access to the analysis service. The class is a normal typescript class, its not part of any module.

export class Stream {
  private exchange: ccxt.Exchange;
  private market: ccxt.Market;
  private timeframe: string;
  private ohlcv_cache: ccxt.OHLCV[];
  private createdAt: Date;
  private stream;
  @Inject(AnalysisService)
  private readonly analysisService: AnalysisService;
  constructor(exchange: ccxt.Exchange, market: ccxt.Market, timeframe: string) {
    this.exchange = exchange;
    this.market = market;
    this.timeframe = timeframe;
    this.createdAt = new Date();
    this.initialize();
    console.log(this.analysisService);
  }
  }

When I do this and call a method of the analysis service inside of the Stream class, analysisService is undefined.

4
  • How does this Stream class get created? Commented Nov 22, 2022 at 15:06
  • I have a service which has an Array of Streams[] Class. Inside the same service they get created by pushing new Instances of Stream into the array. Commented Nov 22, 2022 at 15:08
  • So you call new Stream() yourself? Commented Nov 22, 2022 at 15:15
  • yes. They are just simple es6 classes I create by /POST requests which then get stored inside stream.service class inside an array of Type Stream[] Commented Nov 22, 2022 at 15:17

1 Answer 1

2

As you're calling new Stream() yourself, Nest will do no injection for you. You'd need to either pass the AnalysisService instance yourself, or you'd need to create a setter for that property before running any methods that need the AnalysisService.

Sign up to request clarification or add additional context in comments.

2 Comments

you basically make the Constructor of Stream have a param of analysisService and then on Object creation pass the service instance to the Object constructor?
Yeah. Because if you handle the creation of the class, Nest won't do any injection for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.