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.
If you want to expose all the providers exported by that dynamic module (ElasticsearchModule.registerAsync()
one), you'll need to export ElasticsearchModule
module. See: https://docs.nestjs.com/modules#module-re-exporting
To do so, just add ElasticsearchModule
to the exports
array of elasticSearchDatabaseModule
module.