无法弄清楚我的代码出了什么问题。复查错误描述;我觉得还好。
运行npm start
控制台时说:
嵌套无法解析DescribeService的依赖项 (UrlsAfipService,WsaaService 、?)。请确保论点 ApiModule上下文中的索引为[2]处。
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 {}
答案 0 :(得分:0)
如错误消息所述,无法解析DescribeService
的构造函数的第三个参数,这是:
private clients: object
nestjs中的服务(提供程序)由启用依赖项注入的应用程序上下文创建(实例化)。您可以只声明您需要服务,而无需考虑其依赖关系。当然,这仅在所有依赖项都在应用程序上下文中时才有效。 object
显然不是提供者,因此无法注入。您没有指定clients
是什么,但这是您要声明的方式:
您可以在ApiModules
提供者中提供一系列客户:
export const CLIENTS = 'clients';
@Module({
controllers: [ApiController],
providers: [
UrlsAfipService,
WsaaService,
DescribeService,
{ provide: CLIENTS, useValue: [clientA, clientB] }
],
imports: [ConfigModule]
})
export class ApiModule {}
然后,您可以使用@Inject
装饰器将数组注入服务中:
export class DescribeService {
constructor (
private readonly urlsAfipService: UrlsAfipService,
private readonly wsaaService: WsaaService,
@Inject(CLIENTS) private clients: Client[],
) {}
请查看此answer,以详细了解Nest的DI系统。