Nest.js cant resolve dependencies, can't find my mistake - javascript

I'm doing a Nest.js program but I can't find my dependencies problem. I have searched quite a lot and I have found a lot of answers regarding this problem, but I can't figure out why my code isn´t working. So I have a product module which has his DTO´s, Entity, Controller, Service and module, besides it has an interface for its service.
ProductController
import { Controller, Get } from '#nestjs/common';
import { ProductServiceInterface } from './interface/product.service.interface'
#Controller('products')
export class ProductController {
constructor(private readonly productService: ProductServiceInterface) {}
#Get()
getHello(): string {
return this.productService.test();
}
}
ProductServiceInterface
import { Injectable } from '#nestjs/common';
import {
CreateProductInput,
CreateProductOutput,
} from '../dto/create-product.dto';
import { FindProductOutput } from '../dto/find-product.dto';
export interface ProductServiceInterface {
create(input: CreateProductInput): Promise<CreateProductOutput>;
findProduct(productId: string): Promise<FindProductOutput>;
test();
}
ProductService
import { Injectable } from '#nestjs/common';
import { ProductServiceInterface } from './interface/product.service.interface';
import {
CreateProductInput,
CreateProductOutput,
} from './dto/create-product.dto';
import { Product } from './entity/product.entity';
import { InjectRepository } from '#nestjs/typeorm';
import { Repository } from 'typeorm';
import { FindProductOutput } from './dto/find-product.dto';
//import WooCommerce from '../../config/woocomerce.config';
#Injectable()
export class ProductService implements ProductServiceInterface {
constructor(
#InjectRepository(Product)
private productRepository: Repository<Product>,
) {}
public async create(
productDto: CreateProductInput,
): Promise<CreateProductOutput> {
const product = new Product();
product.name = productDto.name;
product.price = productDto.price;
product.imgUrl = productDto.imgUrl;
const savedProduct = await this.productRepository.save(product);
const productOutput = new CreateProductOutput();
productOutput.id = savedProduct.id;
return productOutput;
}
public async findProduct(idProduct: string): Promise<FindProductOutput> {
const fetchedProduct = await this.productRepository.findOne(idProduct);
const productOutput = new FindProductOutput();
productOutput.id = fetchedProduct.id;
productOutput.imgUrl = fetchedProduct.imgUrl;
productOutput.name = fetchedProduct.name;
productOutput.price = fetchedProduct.price;
return productOutput;
}
public test() {
return 'test'
// WooCommerce.get('products', {
// pero_page: 20,
// }).then((resp) => {
// return resp;
// });
}
}
ProductModule
import { ProductController } from './product.controller';
import { ProductService } from './product.service';
import { Product } from './entity/product.entity';
import { TypeOrmModule } from '#nestjs/typeorm';
import { Module } from '#nestjs/common';
#Module({
imports: [TypeOrmModule.forFeature([Product])],
controllers: [ProductController],
providers: [ProductService],
})
export class ProductModule {}
AppModule
import { Module } from '#nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '#nestjs/config';
import configuration from 'src/config/configuration';
import { TypeOrmModule } from '#nestjs/typeorm';
import { TypeOrmConfigService } from 'src/config/typeorm.config.service';
import { ProductModule } from './modules/product/product.module';
#Module({
imports: [
ConfigModule.forRoot({
load: [configuration],
isGlobal: true,
}),
TypeOrmModule.forRootAsync({
useClass: TypeOrmConfigService,
}),
ProductModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
I hope this code is enough for knowing where my mistake is, I don't like letting others just resolve this for me but I've been watching my code for hours and can't know how to resolve this dependencies problem.

Interfaces disappear at runtime and becomes {} or Object. Nest uses the parameter type metadata to determine what is supposed to be injected (usually via ClassType.name). You have two options to solve this
Use an abstract class instead of an interface. This makes the class still visible at runtime so ClassType.name still works.
Use #Inject('CustomToken') as the way to set the metadata for what Nest needs to inject. You then need to make sure register the custom provider using something like
{
provide: 'CustomToken',
useClass: ClassToBeUsed
}
Either of these methods should fix your issue.

Related

Nest can't resolve dependencies in my project

the console says :
Nest can't resolve dependencies of the VisitService (?, DonorService). Please make sure that the argument VisitModel at index [0] is available in the VisitModule context.
Potential solutions:
If VisitModel is a provider, is it part of the current VisitModule?
If VisitModel is exported from a separate #Module, is that module imported within VisitModule?
this is vistit.service :
import { forwardRef, Inject, Injectable } from '#nestjs/common';
import { InjectModel } from '#nestjs/mongoose';
import { Model } from 'mongoose';
import { DonorVisitStatus } from 'src/constants/donor-visit-status.enum';
import { GetDateDifferenceHours } from 'src/utils';
import { DonorService } from '../donor/donor.service';
import { VisitDocument } from '../schemas/visit.schema';
import { CreateVisitInput } from './graphql/visit.input';
import { Visit } from './graphql/visit.type';
#Injectable()
export class VisitService {
constructor(
#InjectModel('Visit') private readonly visitModel: Model<VisitDocument>,
#Inject(forwardRef(() => DonorService)) private donorService: DonorService,
) {}
async findVisitById(visitId: string): Promise<VisitDocument | undefined> {
const visit = await this.visitModel
.findOne({ _id: visitId })
.populate('medicalCheckup')
.populate('phlebotomy')
.populate('donor');
return visit || undefined;
}
}
visit.module :
import { Module, forwardRef } from '#nestjs/common';
import { VisitService } from './visit.service';
import { VisitResolver } from './visit.resolver';
import { ConfigModule } from 'config/config.module';
import { MongooseModule } from '#nestjs/mongoose';
import { VisitSchema } from '../schemas/visit.schema';
import { DonorModule } from '../donor/donor.module';
#Module({
imports: [
MongooseModule.forFeature([{ name: 'Visit', schema: VisitSchema }], 'aidea_donation'),
ConfigModule,
forwardRef(() => DonorModule),
],
providers: [VisitService, VisitResolver],
exports: [VisitService],
})
export class VisitModule {}
i have checked it alot but i can't find any error
You use a named connection in MongooseModule.forFeature() so you need to use the same connection in the #InjectModel(). Change your #InjectModel() to #InjectModel('Visit', 'aidea_donation') and it should work

NestJs: How can I inject a Provider that has a constructor with a Mongoose Model?

Hi I have the following code in NestJS.I am using moduleRef and have declared a custom provider named 'blogService'.However I am getting an error that says:
'Nest can't resolve dependencies of the blogService (?). Please make
sure that the argument BlogModel at index [0] is available in the
AppModule context.'
.What exactly am I doing wrong while declaring the custom provider which is leading to this error as it seems that I am injecting the Mongoose Model as well?
app.module.ts
import { BlogService } from './blog/service/blog.service';
import { Blog } from './blog/schema/blog.schema';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '#nestjs/config';
import { MongooseModule, getModelToken } from '#nestjs/mongoose';
#Module({
imports: [
ConfigModule.forRoot({ isGlobal: true, load: [configuration], }),
MongooseModule.forRoot(process.env.DATABASE_URL)
],
controllers: [AppController],
providers: [AppService, {
provide: 'blogService',
useFactory: () => BlogService,
inject: [getModelToken(Blog.name)]
}],
})
app.controller.ts
import { Controller, Get } from '#nestjs/common';
import { ModuleRef } from '#nestjs/core';
import { BlogService } from './blog/service/blog.service';
#Controller()
export class AppController {
constructor(private readonly modelRef: ModuleRef) { }
#Get('blogs')
async getAllBlogs(): Promise<any> {
const response = await this.modelRef.get('blogService', { strict: false }).getAllBlogs();
return response;
}
}
blog.service.ts
import { Injectable, HttpException, HttpStatus, Logger } from '#nestjs/common';
import { Blog } from '../schema/blog.schema';
import { InjectModel } from '#nestjs/mongoose';
import { Model, Types } from 'mongoose';
#Injectable()
export class BlogService {
private readonly logger = new Logger(BlogService.name);
constructor(#InjectModel(Blog.name) private blogModel: Model<Blog>) { }
async getAllBlogs() {
try {
const blogs = await this.blogModel.find().exec();
return blogs;
} catch (error) {
this.logger.error(error.message);
}
}
}
Not really sure why you're making a custom provider for this, other than possibly academic purposes. Either way, for anything you want to #InjectModel() or use getModelToken() for, you needx to have a MongooseModule.forFeature() call to register the custom provider the #nestjs/mongoose package will create for you. Once you have this you can use #InjectModel() or a custom provider like
{
provide: 'blogService',
inject: [getModelToken(Blog.name)],
useFactory: (model: Model<Blog>) => new BlogService(model)
}

NestJS - Error: Unknown authentication strategy "local"

These are the partial NestJS code snippets I have. I am trying to implement the passport local strategy for getting the username and password. I am getting -Error: Unknown authentication strategy "local", in the controller file when using the auth guard.
AuthModule.ts
import { Module } from '#nestjs/common';
import { JwtModule } from '#nestjs/jwt';
import { PassportModule } from '#nestjs/passport';
import { UserModule } from 'src/user/user.module';
import { JwtAuthController } from './jwt-auth.controller';
import { JwtAuthService } from './jwt-auth.service';
import { JwtStrategy } from './jwt.strategy';
import { LocalStrategy } from './local.strategy';
#Module({
imports: [
UserModule,
PassportModule,
JwtModule.register({
secret: process.env.SECRETKEY,
signOptions: { expiresIn: '3600s' }
})
],
controllers: [JwtAuthController],
providers: [JwtAuthService, LocalStrategy, JwtStrategy],
exports: [JwtAuthService],
})
export class JwtAuthModule {}
local.strategy.ts
import { Strategy } from 'passport-local';
import { PassportStrategy } from '#nestjs/passport';
import { Injectable, UnauthorizedException } from '#nestjs/common';
import { JwtAuthService } from './jwt-auth.service';
#Injectable()
export class LocalStrategy extends PassportStrategy(Strategy, 'local') {
constructor(private authService: JwtAuthService) {
super();
}
async validate(username: string, password: string): Promise<any> {
const user = await this.authService.validateUser({username, password});
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
app.controller.ts
import { Body, Controller, Get, Post, Req, UnauthorizedException, UseGuards } from '#nestjs/common';
import { AuthGuard } from '#nestjs/passport';
import { JwtAuthService } from './jwt-auth/jwt-auth.service';
#Controller()
export class AppController {
constructor(private readonly authService: JwtAuthService) {}
#UseGuards(AuthGuard('local'))
#Post('/auth/login')
async login(#Req() req) {
return this.authService.login(req.user)
}
}
I am getting the following error on calling /auth/login API
[Nest] 26753 - 10/31/2021, 22:08:18 ERROR [ExceptionsHandler] Unknown authentication strategy "local"
Error: Unknown authentication strategy "local"
Am I missing anything? Thanks in advance.
just add the PassportModule and LocalStrategy in app.module .it will be fixed
#Module({
imports: [PassportModule],
providers: [AuthService,LocalStrategy],
})
export class AppModule {}
Fix Bug https://github.com/nestjs/nest/issues/4646
File AUTH.ts
enter code here
import { Module } from '#nestjs/common';
import { JwtModule } from '#nestjs/jwt';
import { PassportModule } from '#nestjs/passport';
import { UserModule } from 'src/user/user.module';
import { JwtAuthController } from './jwt-auth.controller';
import { JwtAuthService } from './jwt-auth.service';
import { JwtStrategy } from './jwt.strategy';
import { LocalStrategy } from './local.strategy';
#Module({
imports: [
UserModule,
PassportModule.register({defaultStrategy:'local'}),
JwtModule.register({
secret: process.env.SECRETKEY,
signOptions: { expiresIn: '3600s' }
})
],
controllers: [JwtAuthController],
providers: [JwtAuthService, LocalStrategy, JwtStrategy],
exports: [JwtAuthService],
})
export class JwtAuthModule {}
Such an old question with no answers...
In my case I was having sort of an cyclic dependency issue. Or at least it was what it looked like. Cyclic deps can be either on nest side or even on node.js require side (on the second case we end up with empty imports).
Case 1:
#Injectable()
export class MyLocalStrategy extends PassportStrategy(PassportLocalStrategy) {
hello = 'hello'
constructor(private authService: AuthService) {
console.log('load local strategy')
}
}
#Module({
imports: [
CommonModule,
PassportModule,
JwtModule.registerAsync({
async useFactory(config: ConfigService) {
const jwtSecret = config.get('APP_KEY')
const expiresIn = config.get('AUTH_TOKEN_EXPIRED')
return {
secret: jwtSecret,
signOptions: {
expiresIn,
},
}
},
inject: [ConfigService],
}),
],
providers: [
RoleService,
JwtStrategy,
JwtService,
AuthService,
],
controllers: [AuthController],
exports: [AuthService],
})
export class AuthModule {
constructor(private moduleRef: ModuleRef) {}
onModuleInit() {
const moduleRef = this.moduleRef
console.log('init auth module')
const local = moduleRef.get(MyLocalStrategy)
const auth = moduleRef.get(AuthService)
console.log('my modules', { local, auth }, local?.hello)
}
}
In this case the "load local strategy" never got logged. Yet "my modules" logged an empty localStrategy instance, without my "hello" property. Weird!
I hacked a fix by moving the class instantiation to a factory provider, and by requiring the dependencies with ModuleRef. The following excerpt worked fine.
#Module({
imports: [
CommonModule,
PassportModule,
JwtModule.registerAsync({
async useFactory(config: ConfigService) {
const jwtSecret = config.get('APP_KEY')
const expiresIn = config.get('AUTH_TOKEN_EXPIRED')
return {
secret: jwtSecret,
signOptions: {
expiresIn,
},
}
},
inject: [ConfigService],
}),
],
providers: [
RoleService,
JwtStrategy,
JwtService,
AuthService,
{
provide: MyLocalStrategy,
useFactory: (moduleRef: ModuleRef) => {
const auth = moduleRef.get(AuthService)
console.log('local strat factory', { auth })
return new MyLocalStrategy(auth)
},
inject: [ModuleRef],
},
],
controllers: [AuthController],
exports: [AuthService],
})
export class AuthModule {
constructor(private moduleRef: ModuleRef) {}
onModuleInit() {
const moduleRef = this.moduleRef
console.log('init auth module')
const local = moduleRef.get(MyLocalStrategy)
const auth = moduleRef.get(AuthService)
console.log('local', { local, auth }, local?.hello)
}
}
I also tried factory + injecting AuthService directly (through providers array, instead of using ModuleRef). I got a null AuthService.
Probably some dependency cycle on the node.js module imports, tough neither eslint or nest wouldn't say anything.
Check to see if your auth module is being imported in your entry file. app.module.ts.
My auth-module.ts
import { Module } from '#nestjs/common';
import { PassportModule } from '#nestjs/passport';
import { UsersModule } from 'src/users/users.module';
import { AuthService } from './auth.service';
import { LocalStrategy } from './local.strategy';
#Module({
imports:[UsersModule,PassportModule],
providers: [AuthService,LocalStrategy]
})
export class AuthModule {}
My app module
import { Module } from '#nestjs/common';
import { PassportModule } from '#nestjs/passport';
import { UsersModule } from 'src/users/users.module';
import { AuthService } from './auth.service';
import { LocalStrategy } from './local.strategy';
#Module({
imports:[UsersModule,PassportModule],
providers: [AuthService,LocalStrategy]
})
export class AuthModule {}
NestJs encourages scalability and therefore organizes your code in modules.
Instead of calling JwtAuthService method in the AppController, do it in the JwtAuthController. Your code should look like this:
import { Body, Controller, Get, Post, Req, UnauthorizedException, UseGuards } from '#nestjs/common';
import { AuthGuard } from '#nestjs/passport';
import { JwtAuthService } from './jwt-auth.service';
#Controller('auth')
export class JwtAuthController {
constructor(private readonly authService: JwtAuthService) {}
#Post('/login')
#UseGuards(AuthGuard('local'))
async login(#Req() req) {
return req.user
}
}
Nest will then instantiate all providers in the JwtAuthModule such that it will be available in it's controller (JwtController).
You Can always optimize your code by moving AuthGuard to it own file local.guard.ts given you the possibility to do any additional business logic.
import { ExecutionContext } from '#nestjs/common';
import { AuthGuard } from '#nestjs/passport';
import { Request } from 'express';
export class LocalGuard extends AuthGuard('local') {}
You need to import the module that implements the authentications.
It can be inside app.module.ts or inside another module that is connected to it.
import { Module } from '#nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { UsersModule } from './users/users.module';
#Module({
imports: [UsersModule, AuthModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
There is a workaround that may clarify things better
https://docs.nestjs.com/techniques/authentication#request-scoped-strategies
I have the same issue with you. My solution is importing LocalStrategy class to module file, in which I use AuthGuard for API endpoint.
For example: in file user.controller.ts I have AuthGuard('local') in the endpoint /login with method POST
//user.controller.ts
#UseGuards(AuthGuard('local'))
#Post('login')
async login (#Request() req: any) {
return req.user;
}
In my case, I just add LocalStrategy class (I also have this class in local.strategy.ts like you) in module file:
//user.module.ts
#Module({
imports: [
...
LocalStrategy,
],
controllers: [UserController],
...
}
Conclusion, NestJs is quite complicated in importing modules, don't forget to import what you want into other modules. Good luck!

Nest can't resolve dependencies of the RestController

I'm working in a new Nest.js project and whenever I try to start the server:
[ExceptionHandler] Nest can't resolve dependencies of the RestController (?). Please make sure that the argument dependency at index [0] is available in the RestModule context.
I've checked the documentation and looked everywhere and i cannot spot the problem. The RestController class injects UsersService, which is exported by UsersModule and imported in RestModule. Everything looks fine but didn't work.
This is my code:
app.module.ts:
import { Module } from '#nestjs/common'
import { RestModule } from './rest/rest.module'
#Module({
imports: [
RestModule
]
})
export class AppModule { }
rest.module.ts:
import { Module } from '#nestjs/common'
import { RestController } from './rest.controller'
import { UsersModule } from '../users/users.module'
#Module({
imports: [
UsersModule
],
controllers: [
RestController
]
})
export class RestModule { }
rest.controller.ts:
import { Controller, Inject, Get } from '#nestjs/common'
import { UsersService } from '../users/users.service'
import { User } from '../users/user.interface'
#Controller()
export class RestController {
constructor (
#Inject()
private readonly usersService: UsersService
) { }
#Get()
async getAll (): Promise<User[]> {
return this.usersService.findAll()
}
}
users.module.ts:
import { Module } from '#nestjs/common'
import { UsersService } from './users.service'
#Module({
providers: [
UsersService
],
exports: [
UsersService
]
})
export class UsersModule { }
users.service.ts:
import { Injectable } from '#nestjs/common'
import { User } from './user.interface'
#Injectable()
export class UsersService {
async findAll (): Promise<User[]> {
return new Promise((resolve, reject) => resolve([]))
}
}
Thank you in advance!
In your RestController class you have to remove the #Inject() from your constructor:
constructor (
private readonly usersService: UsersService
) { }
#Inject() can be used for non-class-based provider tokens.

How to Inject plain Service/Provider in nest.js

I made a plain typeScript class in nest.js.
JwtTokenService.js
// JwtTokenService.js
import { Injectable, Optional } from '#nestjs/common';
import { JwtService } from '#nestjs/jwt';
import { JwtPayload } from '../modules/auth/interface/jwt-payload.interface';
#Injectable()
export class JwtTokenService {
constructor(private readonly jwtService: JwtService) {}
async generateJWT(payload: object): Promise<string> {
payload['type'] = 'access_token';
const token = this.jwtService.sign({ payload });
return token;
}
}
Now how can I use this in any controller. like user, auth and other.
Register the service in the nest application module:
import { Module } from '#nestjs/common';
import { YourController } from './path-to/your.controller';
import { JwtTokenService } from './path-to/JwtTokenService.service';
#Module({
controllers: [YourController],
providers: [JwtTokenService],
})
export class ApplicationModule {}
Then you can use it in your controller:
import { Controller, Get, Post, Body } from '#nestjs/common';
import { JwtTokenService } from './path-to/JwtTokenService.service';
#Controller('your')
export class YourController {
constructor(private readonly jwtTokenService: JwtTokenService) {}
#Get()
async get() {
// use `this.jwtTokenService`
...
}
}
Nest is using the a DependencyInjection pattern to provide the service to the controller, which is why you need to declare how the service is provided in the application module.

Categories