Utilizing nested services for enhanced functionality

I'm facing an issue with my folder structure:

.
├── lib/
│   └── dma/
│       ├── modules/
│       │   └── cmts/
│       │       ├── cmts.module.ts
│       │       └── cmts.service.ts
│       └── dma.module.ts
└── src/
    ├── app.module.ts
    └── app.service.ts

Within this structure, I have 3 modules and 2 services. The AppService service depends on the CmtsService:

// # ~/src/app.service.ts
@Provider()
class AppService{
  constructor(private readonly cmtsService: CmtsService)
}
// # ~/src/app.module.ts
@Module({
  imports: [DmaModule],
})
class AppModule{}
// # ~/lib/dma/dma.module.ts
@Module({
  imports: [CmtsModule],
})
class DmaModule{}
// # ~/lib/dma/modules/cmts/cmts.module.ts
@Module({
  imports: [],
  providers: [CmtsService]
})
class CmtsModule{}
// # ~/lib/dma/modules/cmts/cmts.service.ts
@Provider()
class CmtsService{
}

Upon running the application, I encounter the following error message:

ERROR [ExceptionHandler] Nest can't resolve dependencies of the AppService (?). Please make sure that the argument CmtsService at index [0] is available in the AppModule context.

Answer №1

The solution required not only exporting the service from the nested CmtsModule but also exporting the CmtsModule itself from the parent module (DmaModule).

Here is the revised code that resolved the issue:

(no changes 🔴)

// # ~/src/app.service.ts
@Provider()
class AppService{
  constructor(private readonly cmtsService: CmtsService)
}

(no changes 🔴)

// # ~/src/app.module.ts
@Module({
  imports: [DmaModule],
})
class AppModule{}

(yes changes 🟢)

// # ~/lib/dma/dma.module.ts
@Module({
  imports: [CmtsModule],
  exports: [CmtsModule], // 👈 Check this out
})
class DmaModule{}

(yes changes 🟢)

// # ~/lib/dma/modules/cmts/cmts.module.ts
@Module({
  imports: [],
  providers: [CmtsService]
  exports: [CmtsService] // 👈 Check this out
})
class CmtsModule{}

(no changes 🔴)

// # ~/lib/dma/modules/cmts/cmts.service.ts
@Provider()
class CmtsService{}

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

Executing multiple service calls in Angular2

Is there a way to optimize the number of requests made to a service? I need to retrieve data from my database in batches of 1000 entries each time. Currently, I have a loop set up like this: while (!done) { ... } This approach results in unnecessary re ...

What could be the cause of this malfunction in the Angular Service?

After creating an Angular app with a controller, I noticed that while I can successfully interact with the controller using Postman (as shown in the screenshot below), I faced issues with displaying data at the frontend. I implemented a new component alon ...

TS2365: The '!== 'operator is not compatible with the types ""("" and "")""

function myFunction(identifier: string) { identifier = "("; }; let identifier: string = ")"; if (identifier !== '(') throw "Expected '(' in function"; myFunction(identifier); if (identifier !== ')') throw "Expected &a ...

Is there a way to transform Observable<Observable<Organization>[]> into Observable<Organization[]>?

I have implemented ngrx/store in my project. .map((p: Observable<Organization>[]) => { return new usersActions.GetOrganizationSuccess(p); }) The GetOrganizationSuccess action is designed to accept Organization[] as the payload. Is ...

What purpose does the array.pop()!(object) syntax serve within Codemirror?

Within the Codemirror 6 documentation and at line 41 of the code, there is a snippet that reads: while (pending.length) pending.pop()!(data.updates) I'm curious about the meaning of this syntax. It appears to be TypeScript specific. How would this lo ...

Issue with React TypeScript: Only the first state update takes effect, despite multiple updates being made

Check out this sandbox I created here. When you leave any of the form inputs blank, I should be seeing 3 errors but instead, I only get one. Can anyone explain why this is happening? import React, { ChangeEvent, useState } from 'react'; import { ...

What is the best way to employ the pick function with optional types within nested TypeScript structures?

I am interested in learning TypeScript. dynamicContent?: { data?: { attributes?: { baccarat?: { title?: string | null; content?: string | null } | null; baccaratOnline?: { title?: string | null; content?: string | null } | null; ...

Exploring a different approach to utilizing Ant Design Table Columns and ColumnGroups

As per the demo on how Ant Design groups columns, tables from Ant Design are typically set up using the following structure, assuming that you have correctly predefined your columns and data: <Table columns={columns} dataSource={data} // .. ...

typescript max recursion depth restricted to 9 levels

After countless attempts, I finally managed to create a generic type that provides me with all possible combinations of JSON key lists and values. Additionally, I have developed a method to limit the recursion within this type. type EditAction<T,P exten ...

Encountering an issue with PrimeNG's <p-calendar> component: the error message "date

I encountered an issue resulting in the following error message: core.es5.js:1020 ERROR Error: Uncaught (in promise): TypeError: date.getMonth is not a function TypeError: date.getMonth is not a function This error occurs whenever I attempt to implement ...

The Vue store array declaration triggers a TS error stating that it is not assignable to a parameter of type never

I'm puzzled as to why this error keeps showing up: Argument of type '{ id: string; }' is not assignable to parameter of type 'never'. ... appearing at const index = state.sections.findIndex((section) => section.id === id); T ...

Querying data elements using Graphql mutations: a step-by-step guide

const MUTATION_QUERY = gql` mutation MUTATION_QUERY( $name: bigint! ) { insert_name( objects: { name: $name } ) { returning { id name } } } `; const [onClick, { error, data }] = useMut ...

The TypeScript error "Issue with Type Assertion: 'This expression is not callable Type'...' has no call signatures" occurs when there is a missing semicolon

Here's a simplified version of our original code: const start: number = 10 const end: number = 20 (someElement as HTMLInputElement).setSelectionRange(start, end) We encountered an error with the 20, where a red squiggly line appeared indicating ...

This problem occurs when using findOneAndUpdate to update an array of objects in an object

I am facing an issue while attempting to update multiple fields of an object in an array. It seems like my code is not working as expected. What could be the problem? Data Example: { _id: 'mongodbid', name: 'something', employees ...

There seems to be an issue with the authorization function in nextauthjs utilizing TypeScript

In my NextJS application utilizing nextAuth with TypeScript, I am encountering difficulties implementing the credentials provider. Below is a snippet from my api\auth\[...nextauth]\route.ts file: CredentialsProvider({ name: 'cre ...

Issue with the code: Only arrays and iterable objects are permitted in Angular 7

Trying to display some JSON data, but encountering the following error: Error Message: Error trying to diff 'Leanne Graham'. Only arrays and iterables are allowed Below is the code snippet: The Data {id: 1, name: "Leanne Graham"} app.compone ...

Prevent the Icon in Material UI from simultaneously changing

I'm working on a table where clicking one icon changes all icons in the list to a different icon. However, I want to prevent them from changing simultaneously. Any suggestions on how to tackle this issue? Code: import React from 'react'; im ...

Progressive series of observable conditions

The issue at hand: I am faced with the task of checking multiple conditions, some of which lead to the same outcome. Here is the current flow: First, I check if a form is saved locally If it is saved locally, I display text 1 to the user If not saved l ...

Am I utilizing Angular's signals correctly? And what is the purpose of still requiring ChangeDetectorRef.detectChanges()?

I have been experimenting with signals and I am questioning whether the current use case would be more beneficial without using Signals and instead just having a "normal" class member swiper?: Swiper: @Component({ selector: '[app-test]', stan ...

After filling a Set with asynchronous callbacks, attempting to iterate over it with a for-of loop does not accept using .entries() as an Array

Encountering issues with utilizing a Set populated asynchronously: const MaterialType_Requests_FromESI$ = SDE_REACTIONDATA.map(data => this.ESI.ReturnsType_AtId(data.materialTypeID)); let MaterialCollectionSet: Set<string> = new Set<s ...