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

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.

Related

Dependency injection is undefined in exported service of 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.

Nest can't resolve dependencies of the movieModel (?). Please make sure that the argument DatabaseConnection at index [0] is available i

I have trouble with using mongoose in nest
error message is below :
Nest can't resolve dependencies of the movieModel (?). Please make sure that the argument DatabaseConnection at index [0] is available in the MongooseModule context.
Potential solutions:
If DatabaseConnection is a provider, is it part of the current MongooseModule?
If DatabaseConnection is exported from a separate #Module, is that module imported within MongooseModule?
movies.service.ts
import { Injectable } from '#nestjs/common';
import { CreateMovieDto } from './dto/create-movie.dto';
import { InjectModel } from '#nestjs/mongoose';
import { movie, movieDocument } from './schema/movie.schema';
import { Model } from 'mongoose';
#Injectable()
export class MoviesService {
constructor(
#InjectModel(movie.name) private movieModel: Model<movieDocument>,
) {}
async create(CreateMovieDto: CreateMovieDto): Promise<movie> {
return new this.movieModel(CreateMovieDto).save();
}
async getAll(): Promise<movie[]> {
return this.movieModel.find().exec();
}
app.modules.ts
import { Module } from '#nestjs/common';
import { MongooseModule } from '#nestjs/mongoose';
import { MoviesModule } from './movies/movies.module';
#Module({
imports: [
MoviesModule,
MongooseModule.forRoot('mongodb://localhost:27017/databasename', {
connectionName: 'movies',
}),
],
controllers: [],
providers: [],
})
export class AppModule {}
movie.module.ts
import { Module } from '#nestjs/common';
import { MongooseModule } from '#nestjs/mongoose';
import { MoviesController } from './movies.controller';
import { MoviesService } from './movies.service';
import { movieSchema } from './schema/movie.schema';
#Module({
imports: [
MongooseModule.forFeature([
{
name: 'movie',
schema: movieSchema,
collection: 'movies',
},
]),
],
controllers: [MoviesController],
providers: [MoviesService], //nestjs가 movieservice를 import하고 controller에 inject 한다.
})
export class MoviesModule {}
please help me figure this out

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

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 can't resolve dependencies of the JobsService (?). Please make sure that the argument JobModel at index [0] is available in the AppModule context

I'm really confused, I got this weird error and I have no idea.
Error: Nest can't resolve dependencies of the JobsService (?). Please make sure that the argument JobModel at index [0] is available in the AppModule context.
app.module.ts
import { Module } from '#nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MongooseModule } from '#nestjs/mongoose';
import { JobsModule } from './jobs/jobs.module';
import { JobsService } from './jobs/jobs.service';
import { JobsController } from './jobs/jobs.controller';
import config from './config/config';
#Module({
imports: [JobsModule, MongooseModule.forRoot(config.mongoURI, {
useFindAndModify: false,
})],
controllers: [AppController, JobsController],
providers: [AppService, JobsService],
})
export class AppModule {}
/jobs/jobs.module.ts
import { Module } from '#nestjs/common';
import { MongooseModule } from '#nestjs/mongoose';
import { JobsService } from './jobs.service';
import { JobsController } from './jobs.controller';
import { JobsSchema } from './schemas/jobs.schema';
#Module({
imports: [MongooseModule.forFeature([{ name: 'Job', schema: JobsSchema }])],
controllers: [JobsController],
providers: [JobsService],
})
export class JobsModule {}
/job/job.controller.ts
import { Controller, Get, Post, Put, Delete, Body, Param } from '#nestjs/common';
import { JobsService } from './jobs.service';
import { IJobs } from './interfaces/jobs.interface';
#Controller('jobs')
export class JobsController {
constructor(private readonly jobsService: JobsService){}
#Get()
findAll():Promise<IJobs[]> {
return this.jobsService.findAll();
}
}
/job/jobs.service.ts
import { Injectable } from '#nestjs/common';
import { InjectModel } from '#nestjs/mongoose';
import { Model } from 'mongoose';
import { IJobSchema } from './schemas/jobs.schema';
import { IJobs } from './interfaces/jobs.interface';
#Injectable()
export class JobsService {
constructor(#InjectModel('Job') private readonly JobsModel: Model<IJobSchema>){}
async findAll():Promise<IJobs[]> {
return await this.JobsModel.find();
}
}
you already declared JobsService in your JobModule, delete it from Appmodule providers
import { Module } from '#nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MongooseModule } from '#nestjs/mongoose';
import { JobsModule } from './jobs/jobs.module';
import { JobsService } from './jobs/jobs.service';
import { JobsController } from './jobs/jobs.controller';
import config from './config/config';
#Module({
imports: [JobsModule, MongooseModule.forRoot(config.mongoURI, {
useFindAndModify: false,
})],
controllers: [AppController, JobsController],
providers: [AppService], // Here..
})
export class AppModule {}

Resources