Зависимости Разрешая Nestjs косяка

Обновить

April 2019

Просмотры

9 раз

0

Не могу понять, в чем проблема моего кода. (Я новичок с nestjs, я пытаюсь узнать его, передавая некоторые приложения к нему). Журнал консоли говорит:

Гнездо не может решить зависимости в UrlsAfipService (?). Пожалуйста, убедитесь, что аргумент с индексом [0] доступен в контексте ApiModule.

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

1 ответы

0

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.