#ERROR : Nest can't resolve dependencies of the ApiGatewayService

8 messages · Page 1 of 1 (latest)

whole jewel
#

I have a micro services architecture which pass through my api-gateway service : authentication and users.

My users and authentication services are both working and logs show everything is OK.

But the log of my ApiGatewayService throws an error :

Error: Nest can't resolve dependencies of the ApiGatewayService (?, AUTHENTICATION_SERVICE). Please make sure that the argument "USER_SERVICE" at index [0] is available in the AppModule context.
api_gateway-1        | 
api_gateway-1        | Potential solutions:
api_gateway-1        | - Is AppModule a valid NestJS module?
api_gateway-1        | - If "USER_SERVICE" is a provider, is it part of the current AppModule?
api_gateway-1        | - If "USER_SERVICE" is exported from a separate @Module, is that module imported within AppModule?
api_gateway-1        |   @Module({
api_gateway-1        |     imports: [ /* the Module containing "USER_SERVICE" */ ]
api_gateway-1        |   })

api-gateway.module.ts :

import { Module, Global } from '@nestjs/common';
import { ApiGatewayService } from './api-gateway.service';
import { ApiGatewayController } from './api-gateway.controller';
import { ClientsModule, Transport } from '@nestjs/microservices';

@Global()
@Module({
  imports: [
    ClientsModule.register([
      {
        name: "USER_SERVICE",
        transport: Transport.TCP,
        options: {
          host: 'users',
          port: 3002,
        },
      },
      {
        name: "AUTHENTICATION_SERVICE",
        transport: Transport.TCP,
        options: {
          host: 'authentication',
          port: 3001, 
        },
      },
    ]),
  ],
  controllers: [ApiGatewayController],
  providers: [ApiGatewayService],
  exports: [ApiGatewayService],
})
export class ApiGatewayModule {}
#

I have imported this module in the app.module.ts and here is my ApiGatewayService :

import { Injectable, Inject, Logger } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { Observable } from 'rxjs';
import { Request } from 'express';

@Injectable()
export class ApiGatewayService {
  
  private readonly logger = new Logger(ApiGatewayService.name);
  
  constructor(
    @Inject('USER_SERVICE') private userService: ClientProxy,
    @Inject('AUTHENTICATION_SERVICE') private authServiceClient: ClientProxy,
  ) {}

It seems either the api-gateway.module.ts does not want to import to app.module.ts or maybe the services are not well configured ? 🤨

If you need more code or infos, please don't hesitate !

soft sequoia
#

looks like you have ApiGatewayService registered in AppModule as well
I don't think you need that

whole jewel
#

I have two modules for the same nest project / service :

#

But yes, if the sub controllers / services are all registered in the sub module and then this one ix exported in the app.module (parent) it should not be necessary. It was to check

#

Ok it seems it was the problem... thanks ! 🙂

I have another question, about the ports of the micro services in a Docker config. Maybe in another post ?

raven frostBOT
#

This post has been marked as resolved. :white_check_mark:
Please read through the conversation and resolution, if you are having the same issue. If you were the original author of the post and the issue is still fresh (within a few days) and you are still have having trouble, continue to reply here. If you are not the original author of the post or the post has aged, start a new thread linking this one as relevant to your problem, providing as much additional information as possible.