AXIOS_INSTANCE_TOKEN在索引[0]处在AppModule上下文中可用。

huangapple go评论146阅读模式
英文:

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&#39;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&#39;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 &#39;@nestjs/axios&#39;;
import { Injectable } from &#39;@nestjs/common&#39;;
import {
    AxiosRequestConfig,
    AxiosResponse,
} from &#39;axios&#39;;
import { firstValueFrom } from &#39;rxjs&#39;;

@Injectable()
export class ItemService {

    constructor(private readonly httpService: HttpService) { }

    // Get methood
    async get&lt;T&gt;(url: string, config?: AxiosRequestConfig): Promise&lt;AxiosResponse&gt; {
        try {
            const response = await firstValueFrom(this.httpService.get&lt;T&gt;(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

huangapple
  • 本文由 发表于 2023年7月20日 18:06:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定