1

Can't figure out what's wrong with my code. Reviewing what error describes; I find things alright.

Running npm start console says:

Nest can't resolve dependencies of the DescribeService (UrlsAfipService, WsaaService, ?). Please make sure that the argument at index [2] is available in the ApiModule context.

DescribeService

import { Injectable } from '@nestjs/common';
import { UrlsAfipService } from './urls-afip.service'
import { WsaaService } from './wsaa.service'
import * as soap from 'soap';

@Injectable()
export class DescribeService {

  constructor (
    private readonly urlsAfipService: UrlsAfipService,
    private readonly wsaaService: WsaaService,
    private clients: object
  ) {}

  private createClientForService(service: string): Promise<any> {
    return new Promise((resolve, reject) => {
      if (this.clients[service]) {
        resolve(this.clients[service])
      } else {
        soap.createClient(this.urlsAfipService.getService(service), (err, client) => {
          if (err && !client) {
            reject(err)
          } else {
            this.clients[service] = client;
            resolve(client);
          }
        })
      }
    })
  }

  describe(service) {
        this.wsaaService.generateToken(service).then((tokens) => {

            this.createClientForService(service).then((client) => {
                return client.describe()
            });

        }).catch((err) => {
            return err.message
        })
  }

}

ApiModule

import { Module } from '@nestjs/common';
import { ConfigModule } from '../config/config.module';
import { ApiController } from './api.controller';
import { UrlsAfipService } from './urls-afip.service'
import { WsaaService } from './wsaa.service'
import { DescribeService } from './describe.service';

@Module({
  controllers: [ApiController],
  providers: [UrlsAfipService, WsaaService, DescribeService],
  imports: [ConfigModule]
})

export class ApiModule {}
Kim Kern
  • 54,283
  • 17
  • 197
  • 195
Marcelo J Forclaz
  • 718
  • 1
  • 8
  • 27

1 Answers1

2

As the error message says, the third parameter of your DescribeService's constructor cannot be resolved, and this is:

private clients: object

Services (providers) in nestjs are created (instantiated) by the application context which enables dependency injection. You can just declare that you need a service without thinking about its dependencies. This, of course, only works when all dependencies are within the application context. object clearly isn't a provider and hence it cannot be injected. You didn't specify what clients is but this is how you would declare it:

You can provide an array of clients in your ApiModules providers:

export const CLIENTS = 'clients';

@Module({
  controllers: [ApiController],
  providers: [
    UrlsAfipService,
    WsaaService,
    DescribeService,
    { provide: CLIENTS, useValue: [clientA, clientB] }
  ],
  imports: [ConfigModule]
})
export class ApiModule {}

Then you can inject the array in your service with the @Inject decorator:

export class DescribeService {

  constructor (
    private readonly urlsAfipService: UrlsAfipService,
    private readonly wsaaService: WsaaService,
    @Inject(CLIENTS) private clients: Client[],
  ) {}

Have a look at this answer for a more detailed explanation of nest's DI system.

Kim Kern
  • 54,283
  • 17
  • 197
  • 195