[英]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).
(我是 nestjs 的新手,我试图通过向它传递一些应用程序来学习它)。 Console log says:
控制台日志说:
Nest can't resolve dependencies of the UrlsAfipService (?).
Nest 无法解析 UrlsAfipService (?) 的依赖关系。 Please make sure that the argument at index [0] is available in the ApiModule context.
请确保索引 [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 {}
You have declared AfipUrls
as a dependency for UrlsAfipService
but it is not provided in any module.您已声明
AfipUrls
作为一个依赖UrlsAfipService
但它没有任何模块中提供。
So you have to add AfipUrls
to the providers
array of your ApiModule
.所以,你必须添加
AfipUrls
到providers
的阵列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
.考虑创建一个
ConfigService
来封装使用dotenv
从环境变量或.env
文件中读取的环境特定变量。 See this answer for more information.有关更多信息,请参阅此答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.