首页 > 解决方案 > Nest 无法解析 searchService (?) 的依赖关系。请确保索引处的参数

问题描述

我正在使用 Nestjs 框架来开发我的弹性服务应用程序。我在我的代码中使用“@nestjs/elasticsearch”库,我只是想建立数据库连接并在所有其他模块中使用。请在此处找到我的代码示例。

我的应用模块如下所示

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from './config/config.module';
import { DatabaseModule } from './database/database.module';
import { LayoutmgmtModule } from './layoutmgmt/layoutmgmt.module';

@Module({
  imports: [ConfigModule,DatabaseModule, LayoutmgmtModule],
  controllers: [AppController],
  providers: [AppService]  
})
export class AppModule {}

我的数据库模块是,

import { Module } from '@nestjs/common';
import { ElasticsearchModule } from '@nestjs/elasticsearch';
import {ConfigModule} from '../config/config.module';
import {ConfigService} from '../config/config.service';
import {DatabaseService} from './database.service';

@Module({
  imports:[ElasticsearchModule.registerAsync({      
    imports:[ConfigModule],        
    useFactory: async (configService: ConfigService) => ({
      host: configService.get('ELASTIC_URL'),
      log: 'trace', 
      requestTimeout: 3000     
    }),
    inject:[ConfigService] 
  })],   
  providers:[DatabaseService], 
})
export class DatabaseModule {}

我的数据库服务是,

import { Injectable,HttpException } from '@nestjs/common';
import { ElasticsearchService } from '@nestjs/elasticsearch';
import { Client } from 'elasticsearch';

@Injectable()
export class DatabaseService {
   private readonly esClient:Client;
    constructor(private readonly elasticsearchService: ElasticsearchService) {
        try {            
            this.esClient = elasticsearchService.getClient();
            this.esClient.ping({ requestTimeout: 3000 },function(err,res,status){
                if (err || !(res)) {
                    console.log('Unable to connect to the server. Please start the server. Error:', err);
                    throw new HttpException({
                        status: 'error',
                        message: 'Unable to connect to the server. Please start the server. Error:'
                     }, 500);                   
                } else {
                    console.log('Connected to Server successfully!',res, status);
                }
            });
        }
        catch(err) { 
                console.log('Error in connection' + err);
                throw new HttpException({
                    status: 'error',
                    message: 'Unable to reach Elasticsearch cluster'
                 }, 500);
        }           
    }  

}

现在上面我已经初始化了连接并且它没有问题地连接到数据库,但是我试图在另一个称为布局模块的模块/服务中重新使用 ElasticsearchService

布局模块如下所示

import { Module } from '@nestjs/common';
import { LayoutmgmtController } from './layoutmgmt.controller';
import { LayoutmgmtService } from './layoutmgmt.service';

@Module({    
  controllers: [LayoutmgmtController],
  providers: [LayoutmgmtService],  
})
export class LayoutmgmtModule {}

布局服务如下所示

import { Inject, Injectable, Dependencies } from '@nestjs/common';
import { ElasticsearchService } from '@nestjs/elasticsearch';
import { Client } from 'elasticsearch';

@Injectable()
export class LayoutmgmtService {
    private readonly esClient:Client;
    constructor(@Inject(ElasticsearchService) private readonly elasticsearchService: ElasticsearchService) {
        this.esClient = elasticsearchService.getClient();
        if (!this.esClient){
            console.log("Elastic alreayd connected")
        }
    }

}

如果我在构造函数内的上述服务中使用 ElasticSErachService,我会收到以下错误,我想重用现有的连接..

[Nest] 10724 - 2019 年 10 月 14 日,下午 4:50:41 [ExceptionHandler] Nest 无法解析 LayoutmgmtService (?) 的依赖项。请确保索引 [0] 处的参数在 LayoutmgmtModule 上下文中可用。+40ms 错误:Nest 无法解析 LayoutmgmtService (?) 的依赖关系。请确保索引 [0] 处的参数在 LayoutmgmtModule 上下文中可用。在 Injector.lookupComponentInExports (C:\Subu\Elastic\elastic-nest-js\node_modules@nestjs\core\injector\injector.js:183:19) 在 process._tickCallback (internal/process/next_tick.js:68:7 ) 在 Function.Module.runMain (internal/modules/cjs/loader.js:744:11) 在 Object. (C:\Subu\Elastic\elastic-nest-js\node_modules\ts-node\src\bin.ts:158:12) 在 Module._compile (internal/modules/cjs/loader.js:688:30) 在Object.Module._extensions..

标签: nestjs

解决方案


LayoutmgmtModule并且DatabaseModule在您的代码中无论如何都不相关。您已注册但未注册,ElasticsearchModule因此无法找到该服务。DatabaseModuleLayoutmgmtModule

解决方案 1

LayoutmgmtModule您可以通过添加LayoutmgmtControllerLayoutmgmtServicein来摆脱DataBaseModule它,它应该开始工作

解决方案 2

您可以通过在此处提到的装饰器之前DataBaseModule添加来使其成为全局@Global()@Module


推荐阅读