I follwed Nest JS Crash tutorial, Youtube Link, I followed this, but when i import interface in service it shows error
Nest can't resolve dependencies of the ItemsService (?). Please make sure that the argument at index [0] is available in the AppModule context.
I i cloned repository given in tutorial, it is working fine, but when i copy src folder of that repository to my project it then throws error. here is my Service file
import { Injectable } from '@nestjs/common';
import { Item } from './interfaces/item.interface';
import { Model } from 'mongoose';
import { ItemsModule } from './items.module'
import { InjectModel } from '@nestjs/mongoose';
@Injectable()
export class ItemsService {
constructor(@InjectModel('Item') private readonly itemModel: Model<Item>) {}
});
}
when I comment constructor line it works fine, I think issue is with this line,
import { Model } from 'mongoose';
because when i hover on this line it shows could not find declaration for this module. I even tried copying package.json file of working code to test but still error remains same
My module Items contains, controller file, service file, module file, dto file, interface file, schema file,
In order to solve it you have to remove the ItemsController and ItemsService imports from the app.module.ts
file.
This was the solution because:
items.module.ts
file so it's not necessary to import
them again in the app.module.ts
file.items.module.ts
you have the next line:
@Module({
imports: [MongooseModule.forFeature([{ name: 'Item', schema: ItemSchema }])],
...
})
which is necessary to make the dependency injection in the items.service.ts
file works, as you can check in the
app.module.ts
file you don't have that line.app.module.ts
file but then there's no sense to create the
items.module.ts
file.Check my repo xD.
Please remove ItemsController
and ItemsService
from AppModule
. No need include them in AppModule
because you already import ItemsModule
into AppModule
.
app.module.ts
import { AppService } from './app.service';
import { ItemsModule } from "./items/items.module";
import { MongooseModule} from '@nestjs/mongoose';
import config from './config/keys';
@Module({
imports: [ItemsModule, MongooseModule.forRoot(config.mongoURI)],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
items.service.ts
import { Injectable } from '@nestjs/common';
import { Item } from './interfaces/items.interface';
import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
@Injectable()
export class ItemsService {
constructor(@InjectModel('Item') private readonly itemModel:Model<Item>){}
async findAll(): Promise<Item[]> {
return await this.itemModel.find();
}
async fineOne(id: string): Promise<Item> {
return await this.itemModel.findOne({_id: id});
}
}
I also faced to the same problem.I removed the ItemsController and ItemsService files from the app.module.ts.now it works fine.so the app.module.ts looks like this.
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ItemsController } from './items/items.controller';
import { ItemsService } from './items/items.service';
import {MongooseModule} from '@nestjs/mongoose';import { ItemsModule } from
'./items/items.module';
import config from './config/keys';
@Module({
imports: [ItemsModule,MongooseModule.forRoot(config.mongoURI)],
controllers: [AppController ],
providers: [AppService ],
})
export class AppModule {}
If you have two modules depends on each others don't import them directly use forwardRef() in both modules
Source Circular dependency
@Module({
imports: [
MongooseModule.forFeature([
{ name: 'User', schema: UserSchema },
]),
forwardRef(() => BusinessModule),
],
providers: [UserService, UserResolver],
exports: [MongooseModule],
})
export class UserModule {}
and in the other module
@Module({
imports: [
MongooseModule.forFeature([
{ name: 'Business', schema: BusinessSchema },
]),
forwardRef(() => UserModule),
],
providers: [BusinessService, BusinessResolver],
exports: [MongooseModule],
})
export class BusinessModule {}
in app module don't use other controller or provider like as UserController
app.module.ts
@Module({
imports: [
MongooseModule.forRoot('mongodb://localhost:27017/nest'),
UserModule,
TasksModule,
AuthModule,
],
controllers: [AppController],
providers: [AppService],
})
and others module like user.module.ts
@Module({
imports: [
MongooseModule.forFeature([
{
name: 'users',
schema: UserSchema,
},
]),
],
controllers: [UserController],
providers: [UserService],
exports: [UserService],
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With