Unable to utilize a service from a module that is shared when leveraging ClientsModule.registerAsync() functionality

In my current project setup, I have developed a shared config module that incorporates multiple config modules. Each of these config modules exports its own service, and the shared module, in turn, exports all the imported modules. At the moment, my application consists of the app module, which imports the shared config module, and the auth module, which also imports the shared config module.

As part of the project, I am utilizing NestJS microservices and attempting to register the ClientsModule using the registerAsync method to access the auth config service in my auth module.

Here is the directory architecture:

/**
config/  
  - shared-config.module.ts
  - app/
     - app-config.service.ts
     - app-config.module.ts
  - auth/
     - auth-config.service.ts
     - auth-config.module.ts
  ... other config modules
- auth/
   - auth.module.ts
- app/
   - app.module.ts
*/

shared-config.module.ts :

@Module({
  imports: [AppConfigModule, MicroServiceAuthConfigModule, /* and other modules */],
  export: [AppConfigModule, MicroServiceAuthConfigModule, /* and other modules */]
})
export class SharedConfigModule {}

auth-config.module.ts :

@Module({
    imports: [
        ConfigModule.forRoot({
           ... some config
        }),
    ],
    providers: [MicroServiceAuthConfigService],
    exports: [MicroServiceAuthConfigService],
})
export class MicroServiceAuthConfigModule {}

The issue arises when I try to utilize the MicroServiceAuthConfigService to create the ClientsModule within my AuthModule.

auth.module.ts :

@Module({
  imports: [
    SharedConfigModule,
    ClientsModule.registerAsync([
      {
        name: 'AUTH_PACKAGE',
        inject: [MicroServiceAuthConfigService],
        useFactory: (authConfigService: MicroServiceAuthConfigService) => ({
          transport: Transport.GRPC,
          options: {
            url: authConfigService.url,
            package: authConfigService.package,
            protoPath: authConfigService.protoPath,
          },
        }),
      },
    ]),
  ],
  controllers: [AuthController],
  providers: [AuthService],
})
export class AuthModule {}

app.module.ts :

@Module({
  imports: [AuthModule, SharedConfigModule],
})
export class AppModule {}

Despite importing the SharedConfigModule, I encounter an error while trying to access the MicroServiceAuthConfigService within the useFactory. The error message is as follows:

Nest can't resolve dependencies of the AUTH_PACKAGE (?). Please make sure that the argument MicroServiceAuthConfigService at index [0] is available in the ClientsModule context.

Potential solutions:

  • If MicroServiceAuthConfigService is a provider, is it part of the current ClientsModule?
  • If MicroServiceAuthConfigService is exported from a separate @Module, is that module imported within ClientsModule? @Module({ imports: [ /* the Module containing MicroServiceAuthConfigService */ ] })

It is puzzling as I have successfully injected the SharedConfigModule in my app module, and when using

app.get(MicroServiceAuthConfigService)
in my main.ts file, it functions correctly. What might I be doing wrong?

Answer №1

To successfully implement the functionality in your code, make sure to include the necessary imports in your ClientsModule.registerAsync function. Specifically, you should ensure that the array within the imports contains the module that exports the MicroServiceAuthConfigService provider. Here is an example of what your code may need to look like:

@Module({
  imports: [
    SharedConfigModule,
    ClientsModule.registerAsync([
      {
        name: 'AUTH_PACKAGE',
        imports: [MicroServiceAuthConfigModule],
        inject: [MicroServiceAuthConfigService],
        useFactory: (authConfigService: MicroServiceAuthConfigService) => ({
          transport: Transport.GRPC,
          options: {
            url: authConfigService.url,
            package: authConfigService.package,
            protoPath: authConfigService.protoPath,
          },
        }),
      },
    ]),
  ],
  controllers: [AuthController],
  providers: [AuthService],
})
export class AuthModule {}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What's the best way to group rows in an angular mat-table?

I am working on a detailed mat-table with expanded rows and trying to group the rows based on Execution Date. While looking at this Stackblitz example where the data is grouped alphabetically, I am struggling to understand where to place the group header c ...

Leveraging Javascript Modules within a Typescript Vue Application

The issue at hand I've encountered a problem while attempting to integrate https://github.com/moonwave99/fretboard.js into my Vue project. My initial approach involved importing the module into a component as shown below: <template> <div&g ...

Refreshing the Mat Dialog content when removing items in Angular Material

I have successfully implemented a mat dialog table with 3 columns - name, email, and delete icon. When the user clicks on the delete icon, it prompts a confirmation message to confirm the deletion. Upon confirming, the item is removed from the database. Ho ...

The module '@types/googlemaps/index.d.ts' cannot be found

I'm working on integrating the Google Maps API into my Angular project and ran into some issues. Here's what I did to install the necessary npm packages: npm install @agm/core --save-dev npm install @types/googlemaps --save-dev Next, I added th ...

The angular framework is unable to assign a value to the property 'xxxx' because it is currently undefined

I am currently working on a simple application using Ionic (angular) and I am facing an issue with the error message: Cannot set property 'origin' of undefined Below is the interface for Product.ts: export interface Products{ id: number ...

What is the proper type declaration for incoming data from the backend in my TypeScript code when using axios?

In the TypeScript code snippet provided, the type for 'e' (used in the function for form submission) has been figured out. However, a question arises if this type declaration is correct. Additionally, in the catch block, the type "any" is used fo ...

I am unable to locate the appropriate TypeScript type for the `displayName` attribute when it is used as a prop for the `type` component

Having trouble finding the correct type as mentioned in the title. After inspecting with DevTools, I have confirmed that every component I am programmatically looking at has Component.type.displayName. For anything that is not an ElementType, the type is a ...

What is the best way to retrieve the Hash value of an object in Typescript?

What is the process for obtaining the hash value of an object in typescript? For instance: let user:any = {name:'tempuser', age:'29'}; let anotheruser:any = {name:'iam', age:'29'}; if( Object.GetHashCode(user) === ...

Having issues with inline conditional statements in Angular 5

There is a minor issue that I've been struggling to understand... so In my code, I have an inline if statement like this: <button *ngIf="item?.fields?.assetType !== 'tool' || item?.fields?.assetType !== 'questions'">NEXT< ...

Guide on passing a customized component to the title property in Material-UI tooltip

I want to customize a Tooltip component by passing a custom component like the image shown in the link. https://i.stack.imgur.com/QkKcx.png Initially, I used Popover once, but now I prefer Tooltip because of its arrow feature. Currently, I am using Reac ...

Setting up the environment for Angular 7 within a TFS build pipeline

I've been attempting to customize the environment in my tfs build pipeline, but it keeps defaulting to the dev environment. Oddly enough, the 'ng serve' command is working perfectly fine. Below are the version details of my application: An ...

Excessive repetition in the style of writing for a function

When it comes to TypeScript, a basic example of a function looks like this: let myAdd: (x: number, y: number) => number = function ( x: number, y: number ): number { return x + y; }; Why is there redundancy in this code? I'm having trouble g ...

Encountering a Npm ERR! when deploying Angular 6 to Heroku due to missing Start script

I am experiencing an issue with my simple angular 6 app after deploying it to Heroku. When I check the logs using the command heroku logs, I encounter the following error: 2018-07-15T00:45:51.000000+00:00 app[api]: Build succeeded 2018-07-15T00:45:53.9012 ...

How to retrieve values from multiple mat-sliders that are dynamically generated using ngFor loop

Creating multiple mat-sliders dynamically in Angular looks like this: <ng-container *ngFor="let parameter of parameterValues; let i = index;"> <mat-slider (input)="onInputChange($event)" min="1" max="{{ parameter.length }}" step="1" value="1" i ...

Error encountered while attempting to load an image in a React component using TypeScript linting

I am currently working on a React app with Next.js where I have imported an image using the following code: import image1 from '../../../img/dummy-image-2.jpg'; Subsequently, I use the image in my app like so: <img src={image1} alt="Dumm ...

Restoring previous configuration in Ionic2 from the resume() lifecycle method

Encountering an issue with my ionic2 application where I save the last state in local storage when the app goes to the background. Upon resuming, it checks for the value of lastState in local storage and pushes that state if a value exists. The specific er ...

Using Material UI with React and TypeScript

I need some assistance with organizing my menus correctly in React using TypeScript. Currently, they are displaying on top of each other instead of within their respective category menus. I have been struggling to find a solution and would appreciate any h ...

The function that iterates through the 'categoria' state and returns a new array is not functioning properly

Having difficulty with the object of a function using .map(). It works when the code is used directly, but not when put inside a function. For example: if(this.state.cat){ return _.map(this.state.cat, categoria => { if(this.state.search_ ...

How can I retrieve elements from an array that match a certain criteria and update the corresponding objects in

When an array contains matching IDs, the goal is to merge these objects into one object without affecting the original array. The current code only returns matching objects, but the expected result should combine them as described. main.ts const arr = [{ ...

Leverage environment variables within your index.html file

Currently, I am using Angular and I am encountering an issue while attempting to utilize an environment variable in my index.html for Google Analytics. I have attempted the following approach: <script> import {environment} from "./environments/e ...