问题
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:
- You already import ItemsController and ItemsService in your
items.module.ts
file so it's not necessary to import them again in theapp.module.ts
file. - In your
items.module.ts
you have the next line:
which is necessary to make the dependency injection in the@Module({ imports: [MongooseModule.forFeature([{ name: 'Item', schema: ItemSchema }])], ... })
items.service.ts
file works, as you can check in theapp.module.ts
file you don't have that line. - Of course you can add that line to your
app.module.ts
file but then there's no sense to create theitems.module.ts
file.
Check my repo xD.
来源:https://stackoverflow.com/questions/56870498/nest-cant-resolve-dependencies-of-the-itemsservice-please-make-sure-that-t