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

纵然是瞬间 提交于 2020-05-15 11:11:13

问题


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,


回答1:


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.



来源:https://stackoverflow.com/questions/56870498/nest-cant-resolve-dependencies-of-the-itemsservice-please-make-sure-that-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!