Reputation: 1284
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,
Upvotes: 57
Views: 156982
Reputation: 11
My solution was to remove the type imports from all the services and controllers which can sometimes happen when you import things automatically.
import type { ItemsService } from './items/items.service'; // bad
import { ItemsService } from './items/items.service'; // good
Upvotes: 1
Reputation: 1
I also face the same problem, then I remove itemService and itemController line from the controller and provider and the error is solved.
Upvotes: -1
Reputation: 115
I added this in item module and solved
imports: [TypeOrmModule.forFeature([Item])],
Upvotes: 1
Reputation: 89
I've solved it in this way-
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: '',
database: 'nest-react-auth',
entities: [User],
synchronize: true,
}),
TypeOrmModule.forFeature([User]),
],
controllers: [AppController],
providers: [AppService],
})
Here I've added my entity file User - TypeOrmModule.forFeature([User]),
and solved it.
Upvotes: 4
Reputation: 1
please import @ItemModule in your other module where you want to use it. https://docs.nestjs.com/security/authentication
Upvotes: 0
Reputation: 501
I also experienced the same problem. For me I missed the ServiceClass in providers field in one of the submodule. Then I specified it like below and error gone.
@Module({
imports: [MongooseModule.forFeature([{ name: 'test', schema: testSchema }])],
controllers: [TestController],
providers: [TestService],
})
Upvotes: 0
Reputation: 206
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],
})
Upvotes: 10
Reputation: 1134
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 {}
Upvotes: 7
Reputation: 2275
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});
}
}
Upvotes: 19
Reputation: 131
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 {}
Upvotes: 13
Reputation: 1016
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.
Upvotes: 99