Leveraging components exported from a module

There is a UserService that is required by the UserModule and then added to the exports.

import {Module} from '@nestjs/common'
import {TypeOrmModule} from '@nestjs/typeorm'
import {User} from './user.entity'
import {UserService} from './user.service'

@Module({
  imports: [TypeOrmModule.forFeature([User])],
  components: [UserService],
  controllers: [],
  exports: [UserService]
})
export class UserModule{}

Next, an AuthModule needs to utilize the UserService, which is as follows:

import * as passport from 'passport'
import * as PassportAzureAD from 'passport-azure-ad'
import * as session from 'express-session'
import {
  Module,
  NestModule,
  MiddlewaresConsumer,
  RequestMethod,
} from '@nestjs/common'
import {Logger} from '@nestjs/common'
import {UserModule} from '../user/user.module'

@Module({
  imports: [UserModule],
  components: [],
  controllers: []
})
export class AuthModule implements NestModule{
  public configure(consumer: MiddlewaresConsumer){
      // SNIP
      // How to incorporate `UserService` here
  }
}

In what way can the UserService be utilized in this scenario? The documentation explains:

Now each module which would import the CatsModule (we need to put CatsModule into the imports array) has access to the CatsService and will share the same instance with all of the modules which are importing this module too.

Despite this explanation, there isn't a concrete example provided on how to achieve it practically.

Answer №1

To properly utilize the UserService in the context of the AuthModule, it must be imported in the constructor as shown below:

@Module({
  imports: [UserModule],
  components: [],
  controllers: []
})
export class AuthModule implements NestModule{
  constructor(
    private readonly userService: UserService
  ) {}

  public configure(consumer: MiddlewaresConsumer){
    this.userService
  }
}

For more information on dependency injection, please refer to the following resources:

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

Start a HTML 5 video and pause any others playing

On my webpage, I have a series of HTML5 videos listed, but I want to ensure that when one video is played, the others automatically stop or pause. The framework I am using is Angular 2 with TypeScript. <video controls (click)="toggleVideo()" #videoPla ...

There seems to be an issue with the reduction function in TypeScript, where an element is implicitly assigned the 'any' type due to the type of expression

I'm having an issue with the following code which represents grades of students. let students: { [k: number]: string[] } = {}; students[1] = ["Student 1", "Student 2"]; students[2] = ["Student 3", "Student 4"]; console.log(students); Object.ke ...

Is it possible to retrieve the contents of a test file using Jest?

As I continue to write tests for my library, I've noticed that they are turning into a comprehensive documentation guide on how to use the library effectively. I'm curious if Jest offers an API that would enable me to extract the test content in ...

Tips on selecting specific text from a drop-down menu

After struggling to find a solution for retrieving the text of a selected item from a dropdown, I decided to create a common directive. This directive would allow me to easily access the text for all dropdown items when used in my code. Below is the snippe ...

Transform the process.env into <any> type using TypeScript

Need help with handling logging statements: log.info('docker.r2g run routine is waiting for exit signal from the user. The container id is:', chalk.bold(process.env.r2g_container_id)); log.info('to inspect the container, use:', chalk.b ...

Angular 15 is unfortunately not compatible with my current data consumption capabilities

I'm currently facing an issue with Angular 15 where I am trying to access the "content" element within a JSON data. However, when attempting to retrieve the variable content, I am unable to view the elements it contains. import { Component, OnInit } ...

Mapping JSON data from an array with multiple properties

Here is a JSON object that I have: obj = { "api": "1.0.0", "info": { "title": "Events", "version": "v1", "description": "Set of events" }, "topics": { "cust.created.v1": { "subscribe": { ...

Using RadSideDrawer with Typescript in Vue class components: A Step-by-Step Guide

I am attempting to integrate external components into Vue Typescript Class Components. Following the installation of the standard template, I made modifications to its <script> block based on this guide: import { Vue, Component, Prop } from "vue-pro ...

Tips for Crafting a Mutation Response Evaluation

When I execute a graphql mutation, the code looks like this: interface SignInReponse { loginEmail : { accessToken: string; } } const [login] = useMutation<SignInReponse>(LOGIN); This is how the mutation appears in the schema: loginEmail( ...

TypeScript Redux Thunk: Simplifying State Management

Seeking a deeper understanding of the ThunkDispatch function in TypeScript while working with Redux and thunk. Here is some code I found: // Example of using redux-thunk import { Middleware, Action, AnyAction } from "redux"; export interface ThunkDispatc ...

"GraphQL DefinitelyTyped: The go-to resource for TypeScript typings

I have been working on obtaining type definitions for graphql in my project. After installing both graphql and @types/graphql, I am using import * as graphql from "graphql" in my file. Despite this, I am encountering difficulties accessing certain types ...

Encountering an issue when trying to generate a button in Angular

I am currently using JavaScript to dynamically create a button in Angular. While I have been successful in creating the button, I am encountering an error when attempting to change the classname. The error message I am receiving is: Property 'clas ...

Interactive Legend in Highcharts with checkboxes, event handling, stylish hover effects, and customizable symbol sequencing

I'm currently using Highcharts in a Chart component within my application. I need to make some changes to the Legend, so I delved into the documentation and created functions with Highcharts.wrap(). Initially, the Legend was simple with each legend i ...

Steps to automatically set the database value as the default option in a dropdown menu

I'm in need of assistance with a project that I'm working on. To be completely honest, I am struggling to complete it without some help. Since I am new to Angular and Springboot with only basic knowledge, I have hit a roadblock and can't mak ...

Stop additional properties from being added to a typescript interface when converting JSON strings

Currently, I am developing an extension for Arduino on VSCode and facing an issue with a section of my code. To load the project's configuration, I am accessing a .json file located in the .vscode folder. While ideally, the user should not manually ed ...

There is no overload that matches this call in Next.js/Typescript

I encountered a type error stating that no overload matches this call. Overload 1 of 3, '(props: PolymorphicComponentProps<"web", FastOmit<Omit<AnchorHTMLAttributes, keyof InternalLinkProps> & InternalLinkProps & { ...; ...

Permitted the usage of a global variable of any type as the return value of a function that does not explicitly define its

Here's a snippet of TypeScript code that compiles successfully: let testVar: any; const testFunc: () => number = () => { return testVar; }; Why does this code compile without errors? What is the reasoning behind it? ...

Having difficulty accessing an element within ng-template during the unit test writing process with Jasmine

I am encountering an issue when trying to access a button inside an ng-container in my testing environment. Despite manually setting the value of the ngIf condition to true, the elements inside are not being rendered. Here is what I have attempted so far: ...

Create a class with additional attributes to support different types of options

I define a set of options represented by strings: export type Category = 'people' | 'projects' | 'topics' | 'tools' An index is declared as follows: interface Entry { ... } type IPostEntryIndex = { [name in Cate ...

Strategies for storing component data within an Angular service

Recently, I have implemented a dice game feature using Angular. The outcome of the dice rolls is stored in a TypeScript array and then displayed on the HTML page. However, I have been tasked with persisting these results even if I navigate to another pag ...