gpt4 book ai didi

javascript - NestJS无法解析服务mongodb的依赖关系

转载 作者:行者123 更新时间:2023-11-28 16:59:41 25 4
gpt4 key购买 nike

我的 Nest 应用程序在运行时抛出以下错误

Nest can't resolve dependencies of the UserService (?, UserProfileModel). Please make sure that the argument at index [0] is available in the AuthModule context. +3ms
Error: Nest can't resolve dependencies of the UserService (?, UserProfileModel). Please make sure that the argument at index [0] is available in the AuthModule context.
at Injector.lookupComponentInExports (PATH\node_modules\@nestjs\core\injector\injector.js:183:19)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

对于上下文,我正在尝试使用 Mongoose 与 MongoDB Atlas 建立连接。我发现此问题的唯一答案是将 TypeOrmModule.forFeature([UserModel]) 添加到模块导入中。但是,我认为这与此无关,因为 Mongoose 已经这样做了。这是代码片段

认证模块TS

 imports: [
MongooseModule.forFeature([{name: 'User', schema: UserModelSchema}, {name: 'UserProfile', schema: UserProfileModelSchema}])],
controllers: [AuthController],
providers: [UserService],

应用模块 TS

  imports: [AuthModule, MongooseModule.forRoot(config.dbServer)],
controllers: [AppController],
providers: [AppService],
})

认证服务TS

constructor(@InjectModel('Users')private readonly users: Model<UserModel>,
@InjectModel('UserProfile') private readonly userProfile: Model<UserProfileModel>){
}

非常感谢帮助

最佳答案

在您的导入中'User'是单数:

{name: 'User', schema: UserModelSchema}
^^^^

在你的构造函数中'Users'是复数:

@InjectModel('Users')
^^^^^

应该是一样的。

关于javascript - NestJS无法解析服务mongodb的依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57914672/

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