将 ElasticSearch 连接添加到 Nestjs 上的 app.module
我正在尝试在 app.module.ts 上创建与 ElasticSearch 的连接。 我的项目文件夹如下所示:
src
|database
|- elasticsearch-database.module.ts
|- es.services.ts
|services
|- products
|-products.controller.ts
|-products.module.ts
|-products.service.ts
app.module.ts
首先,我在 elasticsearch-database.module.ts
上创建连接,
@Global()
@Module({
imports: [
ElasticsearchModule.registerAsync({
useFactory: () => ({
node: 'http://localhost:9200'
})
})
],
providers: [SearchService],
exports: [SearchService]
})
export class elasticSearchDatabaseModule{}
我的 SearchService 位于 es.services.ts
@Injectable()
export class SearchService {
constructor(private readonly elasticsearchService: ElasticsearchService) {}
}
然后,我
app.module.ts
@Module({
imports: [
elasticSearchDatabaseModule,
ProductsModule
],
})
export class AppModule {}
在 products.module.ts
上的我的 ProductsModule
@Module({
imports: [
elasticSearchDatabaseModule,
],
controllers: [ProductsController],
providers: [ ProductsService],
})
export class EventsModule { }
上导入 elasticSearchDatabaseModule ,最后在 ProductsService 上导入 elasticSearchDatabaseModule > ,我
import { ElasticsearchService } from "@nestjs/elasticsearch";
@Injectable()
export class EventsService {
constructor(
private readonly elasticSearch : ElasticsearchService
) { }
}
发生的错误是:
Potential solutions:
- If ElasticsearchService is a provider, is it part of the current EventsModule?
- If ElasticsearchService is exported from a separate @Module, is that module imported within EventsModule?
@Module({
imports: [ /* the Module containing ElasticsearchService */ ]
})
+3ms
Error: Nest can't resolve dependencies of the EventsService (?). Please make sure that the argument ElasticsearchService at index [0] is available in the EventsModule context.
如果我直接在 products.module.ts
上创建连接,我能够完全编译而不会出现错误:
imports: [
ElasticsearchModule.register({
node: 'http://elasticsearch:9200',
})
]
但我认为如果我想使用那个 elasticSearch 这不是最好的方法连接其他服务。
I'm trying to create a connection with ElasticSearch on my app.module.ts.
My project folder looks like:
src
|database
|- elasticsearch-database.module.ts
|- es.services.ts
|services
|- products
|-products.controller.ts
|-products.module.ts
|-products.service.ts
app.module.ts
First, I'm creating the connection on elasticsearch-database.module.ts
@Global()
@Module({
imports: [
ElasticsearchModule.registerAsync({
useFactory: () => ({
node: 'http://localhost:9200'
})
})
],
providers: [SearchService],
exports: [SearchService]
})
export class elasticSearchDatabaseModule{}
My SearchService is on es.services.ts
@Injectable()
export class SearchService {
constructor(private readonly elasticsearchService: ElasticsearchService) {}
}
Then, I'm importing elasticSearchDatabaseModule on
app.module.ts
@Module({
imports: [
elasticSearchDatabaseModule,
ProductsModule
],
})
export class AppModule {}
My ProductsModule on products.module.ts
@Module({
imports: [
elasticSearchDatabaseModule,
],
controllers: [ProductsController],
providers: [ ProductsService],
})
export class EventsModule { }
And finally, on ProductsService , I have
import { ElasticsearchService } from "@nestjs/elasticsearch";
@Injectable()
export class EventsService {
constructor(
private readonly elasticSearch : ElasticsearchService
) { }
}
The error occuring is:
Potential solutions:
- If ElasticsearchService is a provider, is it part of the current EventsModule?
- If ElasticsearchService is exported from a separate @Module, is that module imported within EventsModule?
@Module({
imports: [ /* the Module containing ElasticsearchService */ ]
})
+3ms
Error: Nest can't resolve dependencies of the EventsService (?). Please make sure that the argument ElasticsearchService at index [0] is available in the EventsModule context.
I am able to fully compile without error if I create the connection directly on products.module.ts
:
imports: [
ElasticsearchModule.register({
node: 'http://elasticsearch:9200',
})
]
But I think it's not the best approach if I want to use that elasticSearch connection in other services.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要公开该动态模块导出的所有提供程序(
ElasticsearchModule.registerAsync()
之一),则需要导出ElasticsearchModule
模块。请参阅:https://docs.nestjs.com/modules#module-re-exporting< /a>为此,只需将
ElasticsearchModule
添加到elasticSearchDatabaseModule
模块的exports
数组即可。If you want to expose all the providers exported by that dynamic module (
ElasticsearchModule.registerAsync()
one), you'll need to exportElasticsearchModule
module. See: https://docs.nestjs.com/modules#module-re-exportingTo do so, just add
ElasticsearchModule
to theexports
array ofelasticSearchDatabaseModule
module.