Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
NestJS无法解析工厂提供的类的依赖项_Nestjs - Fatal编程技术网

NestJS无法解析工厂提供的类的依赖项

NestJS无法解析工厂提供的类的依赖项,nestjs,Nestjs,让我向您介绍AlgoModule: 现在,当我创建一个新服务并将其放入模块中时,让我们假设TestService: 我在这里所做的是,用标记“API”注入IApi,这是我使用apiFactory在AlgoModule中设置的 启动时,我将得到以下错误: Error: Nest can't resolve dependencies of the ApiService(?). Please make sure that the argument dependency at index [0] is

让我向您介绍AlgoModule:

现在,当我创建一个新服务并将其放入模块中时,让我们假设TestService:

我在这里所做的是,用标记“API”注入IApi,这是我使用apiFactory在AlgoModule中设置的

启动时,我将得到以下错误:

Error: Nest can't resolve dependencies of the ApiService(?). Please make sure that the argument dependency at index [0] is available in the AlgoModule context.
我做错了什么

我想知道,因为我在AlgoModule中导入了ConfigModule,所以工厂应该自动拥有它的依赖项

配置服务:

@Injectable()
export class ConfigService {

    serviceMapping = {
        prod: ApiService,
        test: ApiTestService
    };

    getEnv(): string | 'prod' | 'test' {
        return String(process.env.ENV);
    }

    getApiService(): Type<IApi> {
        const env = this.getEnv();
        return this.serviceMapping[env];
    }
}

您是否在全局模块中的某个位置调用了带有isGlobal标志的ConfigModule.forRoot

如果不是,那么您必须这样做,因为ConfigModule不能像您想象的那样工作


ConfigModule.forRoot或forFeature会公开模块的ConfigService令牌,但普通ConfigModule不会公开,除非它是全局的。

ConfigModule是我创建的模块,我没有forRoot。请提供ConfigService的源代码好吗?@csakbalint提供的ConfigService我用一个简单的示例重新创建了您的项目,它运行时没有问题。可能此问题与此代码无关,但在其他地方,您意外地尝试实例化ApiService。在这里你可以找到我的测试:告诉我,如果你发现了什么@我发现了这个问题。这是因为我有serviceMapping,它包含在这些类的类型值中。请参阅我添加的config类,它触发了注入,而这些类型的ConfigService没有可注入的内容。但为什么会这样呢?只是一种类型?我很想知道答案!不,不是。我认为这个问题存在于您提供的代码之外,否则它会出现在我的示例中。在某个地方,您尝试使用ApiService,但不导入ConfigService。
@Injectable()
export class TestService {
    constructor(
        private config: ConfigService,
        @Inject('API') private api: IApi) {
    }
}
Error: Nest can't resolve dependencies of the ApiService(?). Please make sure that the argument dependency at index [0] is available in the AlgoModule context.
@Injectable()
export class ConfigService {

    serviceMapping = {
        prod: ApiService,
        test: ApiTestService
    };

    getEnv(): string | 'prod' | 'test' {
        return String(process.env.ENV);
    }

    getApiService(): Type<IApi> {
        const env = this.getEnv();
        return this.serviceMapping[env];
    }
}