patternjavascriptnestjsCritical
Inject nestjs service from another module
Viewed 0 times
nestjsinjectmodulefromserviceanother
Problem
I've got a
I want to use the
When I add it by injection:
I get this nest error :
[Nest] 11592 - 2018-8-13 11:42:17 [ExceptionHandler] Nest can't
resolve dependencies of the PlayersService (+, ?). Please make sure
that the argument at index [1] is available in the current context.
Both modules are imported in the
PlayersModule and an ItemsModule.I want to use the
ItemsService in the PlayersService.When I add it by injection:
import { Injectable } from '@nestjs/common';
import { InjectModel } from 'nestjs-typegoose';
import { ModelType, Ref } from 'typegoose';
import { Player } from './player.model';
import { Item } from '../items/item.model';
import { ItemsService } from '../items/items.service';
@Injectable()
export class PlayersService {
constructor(
@InjectModel(Player) private readonly playerModel: ModelType,
private readonly itemsService: ItemsService){}I get this nest error :
[Nest] 11592 - 2018-8-13 11:42:17 [ExceptionHandler] Nest can't
resolve dependencies of the PlayersService (+, ?). Please make sure
that the argument at index [1] is available in the current context.
Both modules are imported in the
app.module.ts. Both services are working alone in their module.Solution
You have to export the
and then import the exporting module in the module that uses the service:
⚠️ Don't add the same provider to multiple modules. Export the provider, import the module. ⚠️
ItemsService in the module that provides it:@Module({
controllers: [ItemsController],
providers: [ItemsService],
exports: [ItemsService]
^^^^^^^^^^^^^^^^^^^^^^^
})
export class ItemsModule {}and then import the exporting module in the module that uses the service:
@Module({
controllers: [PlayersController],
providers: [PlayersService],
imports: [ItemsModule]
^^^^^^^^^^^^^^^^^^^^^^
})
export class PlayersModule {}⚠️ Don't add the same provider to multiple modules. Export the provider, import the module. ⚠️
Code Snippets
@Module({
controllers: [ItemsController],
providers: [ItemsService],
exports: [ItemsService]
^^^^^^^^^^^^^^^^^^^^^^^
})
export class ItemsModule {}@Module({
controllers: [PlayersController],
providers: [PlayersService],
imports: [ItemsModule]
^^^^^^^^^^^^^^^^^^^^^^
})
export class PlayersModule {}Context
Stack Overflow Q#51819504, score: 406
Revisions (0)
No revisions yet.