简体   繁体   English

Nestjs - 无法解决服务的依赖关系

[英]Nestjs - Can't resolve dependencies of service

I don't know how to fix this issue.我不知道如何解决这个问题。 I keept trying but can't get my head arround it.我一直在尝试,但无法理解它。

The log throws me this Error: Nest can't resolve dependencies of the ERC20Service (?).日志向我抛出了这个错误:Nest 无法解析 ERC20Service (?) 的依赖关系。 Please make sure that the argument dependency at index [0] is available in the ERC20Module context.请确保索引 [0] 处的参数依赖项在 ERC20Module 上下文中可用。

 import { DynamicModule, Module, Provider } from '@nestjs/common'; import { ERC20Service } from './erc20.service'; const Web3 = require("web3"); export interface ERC20ModuleOptions { global?: boolean; abi: Object, wsEndpoint: string, httpEndpoint: string } export const WEB3_HTTP_TOKEN = 'WEB3_HTTP_TOKEN'; export const WEB3_WS_TOKEN = 'WEB3_WS_TOKEN' export class ERC20Module { static forRoot(options: ERC20ModuleOptions): DynamicModule { const httpProvider: Provider = { provide: WEB3_HTTP_TOKEN, useValue:new Web3(new Web3.providers.HttpProvider(options.httpEndpoint)) }; const wsProvider: Provider = { provide: WEB3_WS_TOKEN, useValue:new Web3(new Web3.providers.WebsocketProvider(options.wsEndpoint)) }; return { module: ERC20Module, providers: [httpProvider, wsProvider, ERC20Service], exports: [ERC20Service], global: options.global, }; } }

 import Web3 from "web3" import { Inject, Injectable } from '@nestjs/common'; import { WEB3_HTTP_TOKEN, WEB3_WS_TOKEN } from './erc20.module'; @Injectable() export class ERC20Service { constructor(@Inject(WEB3_HTTP_TOKEN) private http: Web3,@Inject(WEB3_WS_TOKEN) private ws: Web3) { } }

 import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { ConfigModule } from '@nestjs/config'; import { ERC20Module } from 'src/modules/erc20/erc20.module'; import { abi } from 'src/config/abi'; @Module({ imports: [ ConfigModule.forRoot(), ERC20Module.forRoot({ global: true, httpEndpoint: `https://${process.env.CHAINSTACK_USER}:${process.env.CHAINSTACK_PASSWORD}@${process.env.CHAINSTACK_HTTP_ENDPOINT}`, wsEndpoint: `wss://${process.env.CHAINSTACK_USER}:${process.env.CHAINSTACK_PASSWORD}@${process.env.CHAINSTACK_WS_ENDPOINT}`, abi: abi, }), ], controllers: [AppController], providers: [AppService], }) export class AppModule {}

I must be blind because as far as I see it, I am doing everything the correct way when in comes to injecting, exporting and importing.我一定是瞎了眼,因为在我看来,在注入、导出和导入方面,我所做的一切都是正确的。 Maybe someone on here can guide me along the way to solve this issue.也许这里有人可以指导我解决这个问题。 Thank you in advance先感谢您

Your erc20.service and erc20.module files import from each other, creating a circular dependency.您的erc20.serviceerc20.module文件相互导入,从而形成循环依赖。 Move the constants to a separate file and you should be good to go将常量移动到一个单独的文件,你应该很高兴

// erc20.constants.ts
export const WEB3_HTTP_TOKEN = 'WEB3_HTTP_TOKEN';
export const WEB3_WS_TOKEN = 'WEB3_WS_TOKEN'
// erc20.module.ts
import { DynamicModule, Module, Provider } from '@nestjs/common';
import { WEB3_HTTP_TOKEN, WEB3_WS_TOKEN } from './erc20.constants';
import { ERC20Service } from './erc20.service';
const Web3 = require("web3");

export interface ERC20ModuleOptions {
  global?: boolean;
  abi: Object,
  wsEndpoint: string,
  httpEndpoint: string
}

export class ERC20Module {
  static forRoot(options: ERC20ModuleOptions): DynamicModule {
  
    const httpProvider: Provider = {
      provide: WEB3_HTTP_TOKEN,
      useValue:new Web3(new Web3.providers.HttpProvider(options.httpEndpoint))
    };
    const wsProvider: Provider = {
        provide: WEB3_WS_TOKEN,
        useValue:new Web3(new Web3.providers.WebsocketProvider(options.wsEndpoint))
      };

    return {
      module: ERC20Module,
      providers: [httpProvider, wsProvider, ERC20Service],
    
      exports: [ERC20Service],
      global: options.global,
    };
  }
}
// erc20.service.ts
import Web3 from "web3"
import { Inject, Injectable } from '@nestjs/common';
import { WEB3_HTTP_TOKEN, WEB3_WS_TOKEN } from './erc20.constants';

@Injectable()
export class ERC20Service {
    constructor(@Inject(WEB3_HTTP_TOKEN) private http: Web3,@Inject(WEB3_WS_TOKEN) private ws: Web3) {
    
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM