Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

903
Views
¿Cómo inyectar un servicio nestjs en otro servicio cuando ambos pertenecen al mismo módulo?

Tengo el siguiente escenario en NestJS:

 // user.service.ts @Injectable() export class UserService { constructor( private readonly userRepository: UserRepository, private readonly userProfileService: UserProfileService, ) {} } // user-profile.service.ts @Injectable() export class UserProfileService { constructor( private readonly userProfileRepository: UserProfileRepository, ) {} } // user.module.ts @Module({ imports: [DataRepositoryModule], // Required for the repository dependencies providers: [UserService, UserProfileService], exports: [UserService, UserProfileService], }) export class UserModule {}

Sin embargo, cuando trato de usar UserService dentro de un controlador de otro módulo, aparece el siguiente error:

 Nest can't resolve dependencies of the UserService (UserRepository, ?). Please make sure that the argument dependency at index [1] is available in the UserModule context. Potential solutions: - If dependency is a provider, is it part of the current UserModule? - If dependency is exported from a separate @Module, is that module imported within UserModule? @Module({ imports: [ /* the Module containing dependency */ ] })

Código del controlador:

 @Controller() export class UserController { constructor(private readonly userService: UserService) {} }

Módulo controlador:

 @Module({ imports: [], providers: [], controllers: [UserController], }) export class UserManagementModule {}

Aplicación principal.module.ts:

 @Module({ imports: [ UserManagementModule, UserModule, DataRepositoryModule.forRoot(), ], controllers: [], providers: [], }) export class AppModule {}

Estoy confundido porque estoy haciendo exactamente lo que sugiere el error, agregando ambos servicios dentro de la matriz de proveedores (UserModule). ¿Qué me podría estar perdiendo?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Por su error, parece que hay una dependencia circular entre sus importaciones de archivos. Verifique que no tenga una cadena de importación circular (es decir, el Servicio A importa el Servicio B importa el Servicio A o el Servicio A importa el Módulo A importa el Servicio B importa el Servicio A). Esto puede volverse especialmente común cuando se usan archivos de barril dentro del mismo módulo.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!