【发布时间】:2020-03-12 16:39:02
【问题描述】:
我正在开发一个 nestJs 应用程序,但我无法弄清楚为什么会发生这种情况。我正在使用 mongoose 模块为服务准备我的模型,我已经定义了它并从模式文件中导出。
我已经使用猫鼬模块的导入定义了模型。
@Module({
imports: [MongooseModule.forFeature([{name: 'Message', schema: MessageSchema}])],
providers: [MessageService],
exports: [MessageService]
})
在服务中,我使用@InjectModel() 将消息模型注入到服务中,如下所示:
(Model 从 mongoose 导入,Message 从架构文件导入。
constructor(
@InjectModel('Message') private readonly messageModel: Model<Message>
)
但我仍然有这个错误:
Nest 无法解析 MessageService (?) 的依赖关系。请确保索引 [0] 处的参数 MessageModel 在 MessageService 上下文中可用。
我错过了什么?
【问题讨论】:
-
你有
MongooseModule.forRoot导入吗? -
对不起,原来是我将服务导入依赖模块,而不是模块。问题中没有解决这个问题,我会更新以反映,然后回答问题
-
你找到解决办法了吗,请放在这里。我在同一个问题上苦苦挣扎了 2 天。
标签: dependency-injection nestjs