英文:
AXIOS_INSTANCE_TOKEN at index [0] is available in the AppModule context
问题
我在NestJs项目中使用了Axios,但在Item-module
中使用HttpService
时出现了以下错误:
[Nest] 74973 - 07/20/2023, 3:28:01 PM ERROR [ExceptionHandler] Nest无法解析HttpService的依赖项(?)。请确保索引[0]处的AXIOS_INSTANCE_TOKEN参数在AppModule上下文中可用。
可能的解决方案:
- AppModule是否是有效的NestJS模块?
- 如果AXIOS_INSTANCE_TOKEN是提供者,它是否属于当前的AppModule?
- 如果AXIOS_INSTANCE_TOKEN从单独的@Module导出,那么该模块是否在AppModule中导入?
@Module({
imports: [/*包含AXIOS_INSTANCE_TOKEN的模块*/]
})
错误:Nest无法解析HttpService的依赖项(?)。请确保索引[0]处的AXIOS_INSTANCE_TOKEN参数在AppModule上下文中可用。
我分享了我的代码,请提出在这段代码中的问题。
文件路径:src/app.module.ts
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [configuration]
}),
CamiModule,
ExamplesModule,
ItemModule,
HttpModule
],
controllers: [AppController],
providers: [
AppService,
HttpHandler,
HttpService,
{
provide: APP_INTERCEPTOR, useClass: PerformanceMonitor
}
],
})
export class AppModule {}
文件路径:src/item/item.module.ts
@Module({
imports: [HttpModule],
providers: [ItemService],
exports: [ItemService],
})
export class ItemModule { }
文件路径:src/item/item.service.ts
import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import {
AxiosRequestConfig,
AxiosResponse,
} from 'axios';
import { firstValueFrom } from 'rxjs';
@Injectable()
export class ItemService {
constructor(private readonly httpService: HttpService) { }
// Get方法
async get<T>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse> {
try {
const response = await firstValueFrom(this.httpService.get<T>(url, config));
return response;
} catch(error) {
throw error
}
}
}
英文:
I am using Axios in nestJs project and but I am getting the following error while using HttpService
in Item-module
[Nest] 74973 - 07/20/2023, 3:28:01 PM ERROR [ExceptionHandler] Nest can't resolve dependencies of the HttpService (?). Please make sure that the argument AXIOS_INSTANCE_TOKEN at index [0] is available in the AppModule context.
Potential solutions:
- Is AppModule a valid NestJS module?
- If AXIOS_INSTANCE_TOKEN is a provider, is it part of the current AppModule?
- If AXIOS_INSTANCE_TOKEN is exported from a separate @Module, is that module imported within AppModule?
@Module({
imports: [ /* the Module containing AXIOS_INSTANCE_TOKEN */ ]
})
Error: Nest can't resolve dependencies of the HttpService (?). Please make sure that the argument AXIOS_INSTANCE_TOKEN at index [0] is available in the AppModule context.
I am sharing my code kindly suggestion what is the issue in this code
File-Path: src/app.module.ts
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [configuration]
}),
CamiModule,
ExamplesModule,
ItemModule,
HttpModule
],
controllers: [AppController],
providers: [
AppService,
HttpHandler,
HttpService,
{
provide: APP_INTERCEPTOR, useClass: PerformanceMonitor
}
],
})
export class AppModule {}
File-Path: src/item/item.module.ts
@Module({
imports: [HttpModule],
providers: [ItemService],
exports: [ItemService],
})
export class ItemModule { }
File-Path: src/item/item.service.ts
import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import {
AxiosRequestConfig,
AxiosResponse,
} from 'axios';
import { firstValueFrom } from 'rxjs';
@Injectable()
export class ItemService {
constructor(private readonly httpService: HttpService) { }
// Get methood
async get<T>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse> {
try {
const response = await firstValueFrom(this.httpService.get<T>(url, config));
return response;
} catch(error) {
throw error
}
}
}
答案1
得分: 2
HttpService
提供程序来自@nestjs/axios
将由HttpModule
注册。因此,您不必将其添加到您的providers
数组中。只需删除它,应该可以正常工作。请按照文档操作:https://docs.nestjs.com/techniques/http-modulez
英文:
That HttpService
provider from @nestjs/axios
will be registered by HttpModule
. So you must not add it to your providers
array. Just remove that and it should work. Follow the docs: https://docs.nestjs.com/techniques/http-modulez
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论