Dependency injection is undefined in exported service of Nestjs - nestjs

I have CurrencyService that I want to use in another module. Here's what I did:
import { HttpModule } from '#nestjs/axios';
import { Module } from '#nestjs/common';
import { ScheduleModule } from '#nestjs/schedule';
import { TypeOrmModule } from '#nestjs/typeorm';
import { CurrencyRepository } from './currency.repository';
import { CurrencyService } from './currency.service';
#Module({
imports: [
HttpModule,
ScheduleModule.forRoot(),
TypeOrmModule.forFeature([CurrencyRepository]),
],
exports: [CurrencyService],
providers: [CurrencyService],
})
export class CurrencyModule {}
In my CurrencyService, I have injected a repository and another service:
export class CurrencyService {
constructor(
private currencyRepository: CurrencyRepository,
private httpService: HttpService,
) {}
async getCurrency(base: string, target: string): Promise<Currency> {
console.log(this.currencyRepository);
.....
My problem is that when I import the CurrencyModule, and inject the CurrencyService into the service of another module, currencyRepository and httpService are both undefined.
There is no error on startup, it's just that the dependencies are undefined which causes error on runtime.
I also tried something like this:
import { HttpModule } from '#nestjs/axios';
import { Module } from '#nestjs/common';
import { ScheduleModule } from '#nestjs/schedule';
import { TypeOrmModule } from '#nestjs/typeorm';
import { CurrencyRepository } from './currency.repository';
import { CurrencyService } from './currency.service';
#Module({
imports: [
HttpModule,
ScheduleModule.forRoot(),
TypeOrmModule.forFeature([CurrencyRepository]),
],
exports: [
CurrencyService,
TypeOrmModule.forFeature([CurrencyRepository]),
HttpModule,
],
providers: [CurrencyService],
})
export class CurrencyModule {}
I got the same undefined dependencies.

The #Injectable() decorator is missing from the CurrencyService. This decorator (or any decorator really) is what tells Typescript to emit the metadata of the controller that Nest is reading, so it is crucial to have it in place.

Related

Nest can't resolve dependencies of the AuthController (?). Please make sure that the argument at index [0]

I am trying to add auth controller, to do that I have created auth controller, auth module, and I have imported that to the app module.ts file.. At this time I am getting this error,, How can I solve that?
Auth Controller
import {Body, Controller, Injectable, Post, UnauthorizedException} from '#nestjs/common';
import { Model } from 'mongoose';
import { InjectModel } from '#nestjs/mongoose';
#Controller("login")
export class AuthController {
constructor(
#InjectModel("User") private userModel: Model) {
}
}
Auth Module
import {Module} from '#nestjs/common'
import { MongooseModule } from '#nestjs/mongoose';
import { AuthController } from './auth.controller';
import { UsersSchema } from './users.schema';
#Module({
imports: [
MongooseModule.forFeature([
{
name: "User", schema: UsersSchema
}
])
],
controllers: [
AuthController
]
})
export class AuthModule{
}
App Module
import { Module } from '#nestjs/common';
import { CoursesModule } from './courses/courses.module';
import { MongooseModule } from '#nestjs/mongoose'
import {MONGO_CONNECTION} from './constants'
import { AuthModule } from './auth/auth.module';
#Module({
imports: [
CoursesModule,
AuthModule,
MongooseModule.forRoot(MONGO_CONNECTION)
]
})
export class AppModule {
}
error
Nest can't resolve dependencies of the AuthController (?). Please make sure that the argument at index [0] is available in the AuthModule context

NestJS : cannot provide an instance to child module owning the interface

I try to decouple the controller module of a NestJs module from a domain module (as per clean architecture principles), so that dependency flows only to Domain.
I do not understand how to make it work, injection of the repo interface's implementation does not happen (error when nest starts).
Here is the module structure :
AppModule -> CommunityControllerModule -> DomainModule
(provides repoImpl (has service requiring an unknown
by token) concrete repo interface impl)
The CommunityControllerModule :
import { DomainModule } from 'libs/domain/src';
import { Module } from '#nestjs/common';
import { CommunityController } from './community.controller';
import { CommunityRepositoryImpl } from './community.repository';
#Module({
imports: [DomainModule, CommunityRepositoryImpl],
controllers: [CommunityController],
exports: [CommunityRepositoryImpl],
providers: [
{
provide: 'CommunityRepository',
useClass: CommunityRepositoryImpl,
},
],
})
export class CommunityControllerModule {}
The DomainModule :
import { Module } from '#nestjs/common';
import { CommunityService } from './community.service';
#Module({
providers: [CommunityService],
exports: [CommunityService],
})
export class DomainModule {}
The CommunityService where injection fails :
import { HttpException, HttpStatus, Inject, Injectable } from '#nestjs/common';
import { CommunityRepository } from './community.repo';
#Injectable()
export class CommunityService {
constructor(
#Inject('CommunityRepository') private repo: CommunityRepository,
) {}
}
The outputted error :
> nest start
webpack 5.28.0 compiled successfully in 8218 ms
[Nest] 7204 - 2021-05-05 20:52:05 [NestFactory] Starting Nest application...
[Nest] 7204 - 2021-05-05 20:52:05 [InstanceLoader] CommunityRepositoryImpl dependencies initialized +49ms
[Nest] 7204 - 2021-05-05 20:52:05 [ExceptionHandler] Nest can't resolve dependencies of the CommunityService (?). Please make sure that the argument CommunityRepository at index [0] is available in the DomainModule context.
Potential solutions:
- If CommunityRepository is a provider, is it part of the current DomainModule?
- If CommunityRepository is exported from a separate #Module, is that module imported within DomainModule?
#Module({
imports: [ /* the Module containing CommunityRepository */ ]
})
What I missed was to set the DomainModule as a dynamic module.
The Domain module becomes :
import { DynamicModule, Module } from '#nestjs/common';
import { CommunityService } from './community.service';
#Module({})
export class DomainModule {
static register(repoImpl): DynamicModule {
return {
module: DomainModule,
imports: [repoImpl],
providers: [
CommunityService,
{
provide: 'CommunityRepository',
useClass: repoImpl,
},
],
exports: [CommunityService],
};
}
}
...and the CommunityControllerModule imports it with the repo implementation :
import { DomainModule } from 'libs/domain/src';
import { Module } from '#nestjs/common';
import { CommunityController } from './community.controller';
import { CommunityRepositoryImpl } from './community.repository';
#Module({
imports: [
DomainModule.register(CommunityRepositoryImpl),
CommunityRepositoryImpl,
],
controllers: [CommunityController],
exports: [CommunityRepositoryImpl],
providers: [
{
provide: 'CommunityRepository',
useClass: CommunityRepositoryImpl,
},
],
})
export class CommunityControllerModule {}

Nest can't resolve dependencies of the xService

error (cont): Please make sure that the argument PrismaService at index [0] is available in the Assoc_Page_TagModule context.
There are no errors in the code.
When I attempt > npm run start:dev
I initially get: Found 0 errors. Watching for file changes.
Then an error:
[ExceptionHandler] Nest can't resolve dependencies of the Assoc_Page_TagService (?). Please make sure that the argument PrismaService at index [0] is available in the Assoc_Page_TagModule context.
What is the argument at index [0]? Why is it expecting an argument?
Assoc_Page_Tag.service.ts
import { Injectable } from '#nestjs/common';
import { PrismaService } from '../database';
import { Prisma } from '#prisma/client';
#Injectable()
export class Assoc_Page_TagService {
constructor(private readonly prisma: PrismaService) {}
Assoc_Page_TagsAll() {
return Promise.all([
this.prisma.assoc_Page_Tag.findMany()]).then(([records, total]) => {
return {
records,
metadata: { total },
};
});
}
Assoc_Page_Tag.module.ts
import { Module } from '#nestjs/common';
import { Assoc_Page_TagController } from './Assoc_Page_Tag.controller';
import { Assoc_Page_TagService } from './Assoc_Page_Tag.service';
#Module({
imports: [],
controllers: [Assoc_Page_TagController],
providers: [Assoc_Page_TagService],
exports: [Assoc_Page_TagService],
})
export class Assoc_Page_TagModule {}
Assoc_Page_Tag.controller.ts
import {
Body,Controller,Get,Param,ParseIntPipe,Put,
} from '#nestjs/common';
import { Assoc_Page_TagService } from './Assoc_Page_Tag.service';
import { RESOURCE_BASE_ROUTE } from '../constant';
import { Prisma } from '#prisma/client';
const Route = RESOURCE_BASE_ROUTE.assoc_Page_Tag
#Controller()
export class Assoc_Page_TagController {
constructor(private readonly assoc_page_tagService: Assoc_Page_TagService) {}
#Get(`${Route}`)
all() {
return this.assoc_page_tagService.Assoc_Page_TagsAll();
}
}
app.module.ts
import { Module } from '#nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Assoc_Page_TagModule } from './Assoc_Page_Tag';
#Module({
imports: [
Assoc_Page_TagModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Missing reference to PrismaModule - which exports PrismaService
import { Module } from '#nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PrismaModule } from './database';
import { Assoc_Page_TagModule } from './Assoc_Page_Tag';
#Module({
imports: [
PrismaModule,
Assoc_Page_TagModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}

nest js : Error: Nest can't resolve dependencies of the PlanningService (?)

i'am facing this error when importing planning.module in app.module :
Nest can't resolve dependencies of the PlanningService (?). Please make sure that the argument PlanningMo
del at index [0] is available in the PlanningModule context.
Potential solutions:
- If PlanningModel is a provider, is it part of the current PlanningModule?
- If PlanningModel is exported from a separate #Module, is that module imported within PlanningModule?
#Module({
imports: [ /* the Module containing PlanningModel */ ]
})
this is my planning.module
import { Module } from '#nestjs/common';
import { PlanningController } from './planning.controller';
import { PlanningService } from './planning.service';
import {MongooseModule} from "#nestjs/mongoose";
import {PlanningSchema} from "./schemas/planning.schema";
import {UserModule} from "../user/user.module";
#Module({
imports: [MongooseModule.forFeature([{ name: 'Planing', schema: PlanningSchema }]),UserModule],
providers: [PlanningService],
exports: [PlanningService],
controllers: [PlanningController]
})
export class PlanningModule {}
app.module.ts
import {Module, NestModule, MiddlewareConsumer, RequestMethod} from '#nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MongooseModule } from '#nestjs/mongoose';
import { mongoConfig } from './config';
import { UserModule } from './user/user.module';
import { AuthModule } from './auth/auth.module';
import { JwtMidelware } from './utils/jwtMidelware';
import {UserController} from "./user/user.controller";
import { TeamModule } from './team/team.module';
import { CompanyModule } from './company/company.module';
import { AgenceModule } from './agence/agence.module';
import { PlanningModule } from './planning/planning.module';
#Module({
imports: [
MongooseModule.forRoot(mongoConfig.url, mongoConfig.auth),
PlanningModule,
UserModule,
AuthModule,
TeamModule,
CompanyModule,
AgenceModule,
]
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(JwtMidelware)
.exclude(
{ path: 'users/id', method: RequestMethod.GET },
)
.forRoutes(UserController);
}
}
In your constructor you have #InjectModel('Planning') but in your MongooseModule.forFeature() you have { name:'Planing',...}. These strings need to match.

Dependency Injection for Nest JS Interceptor is Undefined

I have created an interceptor as shown below that I wish to use globally. I added the interceptor to my module and set it up so that nest js should handle the DI for me per NestJS Docs, however when I make a request to my service, I get an error indicating Cannot read property log of undefined so it appears that the DI is not being taken care of by NestJS.
Interceptor Code:
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '#nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { LoggingService } from './logging.service';
#Injectable()
export class AuthInterceptor implements NestInterceptor {
constructor(private readonly loggingService: LoggingService) { }
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next
.handle()
.pipe(
map((response) => {
this.loggingService.log('Responded successfully');
return response;
})
);
}
}
Interceptor Module:
import { Module } from '#nestjs/common';
import { APP_INTERCEPTOR } from '#nestjs/core';
import { AuthInterceptor } from './auth.interceptor';
import { LoggingService } from './logging.service';
#Module({
providers: [
LoggingService,
{
provide: APP_INTERCEPTOR,
useClass: AuthInterceptor,
},
],
})
export class AuthInterceptorModule {}
My app.module.ts at the root of my application imports the AuthInterceptorModule. I am assuming I am messing something up but it is not clear to me how to fix this DI issue.
I was able to resolve this one on my own after finding that my LoggingService relied on another dependency that was request scoped. Since there was a request scoped dependency in my interceptor, this meant that my interceptor also had to be request scoped.
The change to the code was simple and only required that I change the AuthInterceptorModule from:
import { Module } from '#nestjs/common';
import { APP_INTERCEPTOR } from '#nestjs/core';
import { AuthInterceptor } from './auth.interceptor';
import { LoggingService } from './logging.service';
#Module({
providers: [
LoggingService,
{
provide: APP_INTERCEPTOR,
useClass: AuthInterceptor,
},
],
})
export class AuthInterceptorModule {}
to
import { Module } from '#nestjs/common';
import { APP_INTERCEPTOR, Scope } from '#nestjs/core';
import { AuthInterceptor } from './auth.interceptor';
import { LoggingService } from './logging.service';
#Module({
providers: [
LoggingService,
{
provide: APP_INTERCEPTOR,
scope: Scope.REQUEST,
useClass: AuthInterceptor,
},
],
})
export class AuthInterceptorModule {}

Resources