gpt4 book ai didi

typescript - Nest 无法解析 ItemsService (?) 的依赖关系。请确保索引 [0] 处的参数在 AppModule 上下文中可用

转载 作者:行者123 更新时间:2023-12-03 11:23:58 30 4
gpt4 key购买 nike

我遵循了 Nest JS Crash 教程,Youtube Link ,
我跟着这个,但是当我在服务中导入接口(interface)时它显示错误

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



我克隆了教程中给出的存储库,它工作正常,但是当我将该存储库的 src 文件夹复制到我的项目时,它会引发错误。
这是我的服务文件
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>) {}
});

}

当我评论构造函数行时,它工作正常,
我认为问题在于这条线,

import { Model } from 'mongoose';



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

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

最佳答案

为了解决它,您必须从 app.module.ts 中删除 ItemsController 和 ItemsService 导入。文件。

这是解决方案,因为:

  • 您已经在 items.module.ts 中导入了 ItemsController 和 ItemsService文件,因此无需导入
    他们再次在app.module.ts文件。
  • 在您的 items.module.ts你有下一行:

    @Module({
    imports: [MongooseModule.forFeature([{ name: 'Item', schema: ItemSchema }])],
    ...
    })

    这是在 items.service.ts 中进行依赖注入(inject)所必需的文件有效,因为您可以 checkin app.module.ts文件你没有那行。
  • 当然,您可以将该行添加到您的 app.module.ts文件,但没有意义创建items.module.ts文件。

  • 检查我的 repo xD。

    关于typescript - Nest 无法解析 ItemsService (?) 的依赖关系。请确保索引 [0] 处的参数在 AppModule 上下文中可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56870498/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com