Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.1K
Vistas
Inject nestjs service from another module

I've got a 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<Player>,
        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.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You have to export the 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 {}

⚠️ Do not add the same provider to multiple modules. Export the provider, import the module. ⚠️

over 4 years ago · Santiago Trujillo Denunciar

0

Let' say you want to use AuthService from AuthModule in my TaskModule's controller

for that, you need to export authService from AuthModule

@Module({
    imports: [
     ....
    ],
    providers: [AuthService],
    controllers: [AuthController],
    exports:[AuthService]
  })
export class AuthModule {}
  

then in TaskModule, you need to import AuthModule (note: import AuthModule not the AuthService in TaskModule)

@Module({
    imports:[
      AuthModule
    ],
    controllers: [TasksController],
    providers: [TasksService]
  })
export class TasksModule {}

Now you should be able to use DI in TaskController

@Controller('tasks')
export class TasksController {
   constructor(private authService: AuthService) {}
   ...
}
  
over 4 years ago · Santiago Trujillo Denunciar

0

I solved my problem by removing @Inject() from the argument in my constructor that was passing the exported service.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda