Nest无法解析ItemsService(?)的依赖项.请确保AppModule上下文中索引为[0]的参数可用 [英] Nest can't resolve dependencies of the ItemsService (?). Please make sure that the argument at index [0] is available in the AppModule context

查看:643
本文介绍了Nest无法解析ItemsService(?)的依赖项.请确保AppModule上下文中索引为[0]的参数可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注过Nest JS崩溃教程, Youtube链接, 我遵循了这个步骤,但是当我在服务中导入界面时,它显示错误

I follwed Nest JS Crash tutorial, Youtube Link, I followed this, but when i import interface in service it shows error

嵌套不能解析ItemsService(?)的依赖项.请确保AppModule上下文中索引为[0]的参数可用.

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

我克隆了教程中给定的存储库,它工作正常,但是当我将该存储库的src文件夹复制到我的项目中时,它将引发错误. 这是我的服务文件

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';

因为当我将鼠标悬停在这条线上时,它表明找不到该模块的声明. 我什至尝试复制工作代码的package.json文件进行测试,但错误仍然相同

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

我的模块项目包含控制器文件,服务文件,模块文件, dto文件,接口文件,架构文件

My module Items contains, controller file, service file, module file, dto file, interface file, schema file,

推荐答案

要解决该问题,您必须从app.module.ts文件中删除ItemsController和ItemsService导入.

In order to solve it you have to remove the ItemsController and ItemsService imports from the app.module.ts file.

这是解决方案,因为:

  1. 您已经在items.module.ts文件中导入了ItemsController和ItemsService,因此没有必要导入 再次在app.module.ts文件中.
  2. 在您的items.module.ts中,有下一行:
  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 }])],
  ...
})

为了使items.service.ts文件中的依赖项注入有效,这是必需的,因为您可以在 app.module.ts文件没有该行.

  • 您当然可以将该行添加到您的app.module.ts文件中,但是没有必要创建 items.module.ts文件.
  • 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.

  • 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.
  • 检查我的回购 xD.

    这篇关于Nest无法解析ItemsService(?)的依赖项.请确保AppModule上下文中索引为[0]的参数可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆