Nestjs Nest无法解析searchService的依赖项。请确保参数位于索引处

Nestjs Nest无法解析searchService的依赖项。请确保参数位于索引处,nestjs,Nestjs,我正在使用Nestjs框架开发我的弹性服务应用程序。 我在代码中使用“@nestjs/elasticsearch”库,我只是尝试建立数据库连接并在所有其他模块中使用。请在这里找到我的代码示例 我的应用程序模块如下所示 import { AppController } from './app.controller'; import { AppService } from './app.service'; import { ConfigModule } from './config/config.m

我正在使用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 { 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")
        }
    }

}
我的数据库模块是

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);
        }           
    }  

}
现在,我已经初始化了连接,并且连接到数据库时没有问题,但是我正在尝试在另一个名为layout module的模块/服务中重用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 { 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 { 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上下文中可用+40毫秒 错误:嵌套无法解析LayoutmgmtService的依赖项。请确保索引[0]处的参数在LayoutmgmtModule上下文中可用。 在Injector.lookupComponentInExports C:\Subu\Elastic\Elastic nest js\node_modules@nestjs\core\injector\injector.js:183:19 在进程中。_tickCallback internal/process/next_tick.js:68:7 位于Function.Module.runMain internal/modules/cjs/loader.js:744:11 反对。C:\Subu\Elastic\Elastic nest js\node\u modules\ts node\src\bin.ts:158:12 在模块处编译internal/modules/cjs/loader.js:688:30 在Object.Module.\u extensions..js internal/modules/cjs/loader.js:699:10 在Module.load internal/modules/cjs/loader.js:598:32处 在tryModuleLoad internal/modules/cjs/loader.js:537:12 在Function.Module.\u加载内部/modules/cjs/loader.js:529:3 在Function.Module.runMain internal/modules/cjs/loader.js:741:12

中,您没有在任何地方导出ElasticsearchService。也许您的DatabaseModule应该将其与DatabaseService LayoutmgmtService一起导出,而DatabaseService应该使用这两种方式之一。 最重要的是,您应该将给定的服务添加到LayoutmgmtModule的提供程序中,LayoutmgmtModule和DatabaseModule在代码中无论如何都不相关。 您已在DatabaseModule中注册ElasticsearchModule,但未在LayoutmgmtModule中注册,因此无法找到该服务

解决方案1

只需在DataBaseModule中添加LayoutmgmtController和LayoutmgmtService,就可以摆脱LayoutmgmtModule,它应该可以开始工作了

解决方案2

您只需在前面提到的@Module decorator之前添加@global,就可以将DataBaseModule设置为全局的

我有一个类似的问题,@global为我解决了它!