簡體   English   中英

Nestjs - 無法解決服務的依賴關系

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

我不知道如何解決這個問題。 我一直在嘗試,但無法理解它。

日志向我拋出了這個錯誤:Nest 無法解析 ERC20Service (?) 的依賴關系。 請確保索引 [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 {}

我一定是瞎了眼,因為在我看來,在注入、導出和導入方面,我所做的一切都是正確的。 也許這里有人可以指導我解決這個問題。 先感謝您

您的erc20.serviceerc20.module文件相互導入,從而形成循環依賴。 將常量移動到一個單獨的文件,你應該很高興

// 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