简体   繁体   中英

Nestjs cant resolve dependencies

Can't figure out what's the problem of my code. (I'm new with nestjs, I'm trying to learn it by passing some apps to it). Console log says:

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

UrlsAfipService

import { Injectable } from '@nestjs/common';
import { AfipUrls } from './urls'

@Injectable()
export class UrlsAfipService {

  constructor(
    private readonly afipUrls: AfipUrls,
  ) {}

  getWSAA () {
    return this.afipUrls.homo().wsaa; // <- change to prod() for production
  }

  getService (service: string) {
    return this.afipUrls.homo().service.replace('{service}', service)
  }
}

AfipUrls

export class AfipUrls {
    homo() {
      return {
        wsaa: 'https://url.com',
        service: 'https://url.com'
      }
    }

    prod() {
      return {
        wsaa: 'url.com',
        service: 'url.com'
      }
    }
}

ApiModule

import { Module } from '@nestjs/common';
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]
})

export class ApiModule {}

AppModule

import { Module } from '@nestjs/common';

import { ApiModule } from './api/api.module';

import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [ApiModule],
  controllers: [AppController],
  providers: [AppService],
})

export class AppModule {}

You have declared AfipUrls as a dependency for UrlsAfipService but it is not provided in any module.

So you have to add AfipUrls to the providers array of your ApiModule . Then it can be injected.

providers: [UrlsAfipService, WsaaService, DescribeService, AfipUrls]
//                                                         ^^^^^^^^

Note though, that encoding environment specific values in your code base might be a code smell. Consider creating a ConfigService that encapsulates environment specific variables that are read from environment variables or .env files using dotenv . See this answer for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM