Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nest can't resolve dependencies of the ItemsService (?). Please make sure that the argument at index [0] is available in the AppModule context

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,

like image 386
Muhammad Aadil Banaras Avatar asked Jul 03 '19 12:07

Muhammad Aadil Banaras


5 Answers

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:

  1. You already import ItemsController and ItemsService in your items.module.ts file so it's not necessary to import them again in the app.module.ts file.
  2. In your 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.
  3. Of course you can add that line to your app.module.ts file but then there's no sense to create the items.module.ts file.

Check my repo xD.

like image 149
DanielFryy Avatar answered Nov 16 '22 02:11

DanielFryy


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});
    }
}
like image 14
Sumith Ekanayake Avatar answered Nov 16 '22 02:11

Sumith Ekanayake


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 {}
like image 13
Nandika Jeevantha Avatar answered Nov 16 '22 04:11

Nandika Jeevantha


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 {}
like image 6
Ahmed Younes Avatar answered Nov 16 '22 03:11

Ahmed Younes


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],
})
like image 6
Ismail Hosen Avatar answered Nov 16 '22 04:11

Ismail Hosen