NestJS无法重新定义服务mongodb的依赖项

时间:2019-09-12 21:22:35

标签: javascript node.js typescript mongoose nestjs

我的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])添加到模块导入中。但是,我认为这并不重要,因为猫鼬已经做到了。这是代码段

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

非常感谢

1 个答案:

答案 0 :(得分:1)

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

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

,并且在您的构造函数'Users'中是复数:

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

应该一样。