NodeJS NestJS无法解析服务mongodb的依赖项

eh57zj3b  于 2023-10-17  发布在  Node.js
关注(0)|答案(1)|浏览(113)

我的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>){
    }

非常感谢帮助

6ioyuze2

6ioyuze21#

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

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

在构造函数中,'Users'是复数:

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

应该是一样的

相关问题