【问题标题】:NestJS Can't reslove dependencies of service mongodbNestJS 无法解析服务 mongodb 的依赖关系
【发布时间】:2020-01-14 19:13:55
【问题描述】:

我的 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]) 添加到模块导入中。但是,我认为这在这里无关紧要,因为猫鼬已经这样做了。这里是代码sn-ps

身份验证模块 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>){
    }

帮助非常感谢

【问题讨论】:

    标签: javascript node.js typescript mongoose nestjs


    【解决方案1】:

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

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

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

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

    应该是一样的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-19
      • 2018-11-22
      • 2019-12-19
      • 2018-12-25
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      相关资源
      最近更新 更多