nestjs - 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..
解决方案
LayoutmgmtModule
并且DatabaseModule
在您的代码中无论如何都不相关。您已注册但未注册,ElasticsearchModule
因此无法找到该服务。DatabaseModule
LayoutmgmtModule
解决方案 1
LayoutmgmtModule
您可以通过添加LayoutmgmtController
和LayoutmgmtService
in来摆脱DataBaseModule
它,它应该开始工作
解决方案 2
您可以通过在此处提到的装饰器之前DataBaseModule
添加来使其成为全局@Global()
@Module
推荐阅读
- node.js - 为什么在 ORM 挂钩级别访问时,我的 Cls 挂钩当前上下文与以前的上下文相同?
- arrays - 比较和设置列表/数组中的对象的更好方法(前端角度 - 打字稿)?
- android - WebView 在 Android API 23 及以下版本中损坏
- python-3.x - 使用没有错误消息的 tkinter 时 SQLite Python 查询不起作用
- kdb - 序列化外键表如何在 kdb 内部工作
- amazon-web-services - CloudFormation:AWS Glue 分类器报价符号示例值
- linux - 如何将 shell 命令行参数映射到 READ 命令
- c - 我对 n*log(N) Big O 的样子很感兴趣
- php - PHP trim() 的问题 - 从字符串中删除“https://”,这是一个奇怪的问题
- python - PIL 的 ImageFont.getsize() 给出错误的文本大小