Nest 无法解析 UserModel 的依赖项(?)

Posted

技术标签:

【中文标题】Nest 无法解析 UserModel 的依赖项(?)【英文标题】:Nest can't resolve dependencies of the UserModel (?) 【发布时间】:2020-03-12 18:46:01 【问题描述】:

当我尝试在 Users 上使用 MongooseModel 时,出现以下错误

Nest 无法解析 UserModel (?) 的依赖关系。请确保 索引 [0] 处的参数 DatabaseConnection 在 MongooseModule 上下文。

/src/database/database.module.ts

import  Module  from '@nestjs/common';
import  databaseProviders  from './database.providers';
import  ConfigModule  from '../config/config.module';

@Module(
  imports: [ConfigModule],
  providers: [...databaseProviders],
  exports: [...databaseProviders],
)
export class DatabaseModule 

/src/database/database.provider.ts

// NPM Packages
import * as mongoose from 'mongoose';
import  Provider  from '@nestjs/common';
import  MongooseModule  from '@nestjs/mongoose';

// Custom Packages
import  ConfigService  from '../config/config.service';

export const databaseProviders: Provider[] = [
  
    inject: [ConfigService],
    provide: 'DATABASE_CONNECTION',
    useFactory: async (
      configService: ConfigService,
    ): Promise<typeof mongoose> =>
      await mongoose.connect(configService.get('MONGODB_URI'), 
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex: true,
        useFindAndModify: false,
      ),
  ,
];

/src/app.module.ts

// Core Packages
import  Module  from '@nestjs/common';
import  AppController  from './app.controller';
import  AppService  from './app.service';

// NPM Packages

// Custom Packages
import  ConfigModule  from './config/config.module';
import  DatabaseModule  from './database/database.module';
import  AuthModule  from './auth/auth.module';
import  UsersModule  from './users/users.module';

@Module(
  imports: [ConfigModule, DatabaseModule, AuthModule, UsersModule],
  controllers: [AppController],
  providers: [AppService],
)
export class AppModule 

/src/auth/auth.module.ts

// Core Packages
import  Module  from '@nestjs/common';

// NPM Packages
import  PassportModule  from '@nestjs/passport';
import  JwtModule  from '@nestjs/jwt';

// Custom Packages
import  AuthService  from './auth.service';
import  LocalStrategy  from './local.strategy';
import  JwtStrategy  from './jwt.strategy';
import  UsersModule  from '../users/users.module';
import  jwtConstants  from './constants';
import  AuthController  from './auth.controller';
import  ConfigService  from 'src/config/config.service';
import  ConfigModule  from 'src/config/config.module';
import  DatabaseModule  from 'src/database/database.module';

@Module(
  imports: [
    ConfigModule,
    DatabaseModule,
    UsersModule,
    PassportModule,
    JwtModule.registerAsync(
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => (
        secret: configService.get('JWT_SECRET'),
        signOptions:  expiresIn: configService.get('JWT_EXPIRE') ,
      ),
      inject: [ConfigService],
    ),
  ],
  providers: [AuthService, LocalStrategy, JwtStrategy],
  exports: [AuthService],
  controllers: [AuthController],
)
export class AuthModule 

/src/users/user.module.ts

// Core Packages
import  Module  from '@nestjs/common';

// NPM Packages
import  MongooseModule  from '@nestjs/mongoose';

// Custom Packages
import  UsersService  from './users.service';
import  UsersController  from './users.controller';
import  UserSchema  from './schemas/user.schema';
import  DatabaseModule  from 'src/database/database.module';

@Module(
  providers: [UsersService],
  exports: [UsersService],
  controllers: [UsersController],
  imports: [
    DatabaseModule,
    MongooseModule.forFeature([ name: 'User', schema: UserSchema ]),
  ],
)
export class UsersModule 

【问题讨论】:

【参考方案1】:

您应该使用 MongooseModule.forRootAsync 导入,而不是在 database.provider.ts 中静态配置 mongoose:

MongooseModule.forRootAsync(
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => (
    uri: configService.getString('MONGODB_URI'),
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true,
    useFindAndModify: false,
  ),
  inject: [ConfigService],
);

【讨论】:

有没有办法可以将配置部分分离到一个新文件中,然后将其导入app.module.ts 是的,useClass,见docs.nestjs.com/techniques/mongodb#async-configuration 试过了,但不能让它工作。有什么建议? github.com/harshamv/nestjs-starter/blob/master/src/database/… 可能会打开一个关于它的新问题,包括您收到的错误消息。我想说这个线程的问题已经解决了;这似乎是一个新问题。

以上是关于Nest 无法解析 UserModel 的依赖项(?)的主要内容,如果未能解决你的问题,请参考以下文章

Nest 无法解析 AuthService 的依赖项(AuthRepository,?)

Nest 无法解析 JwtService (?) [JWT_MODULE_OPTIONS] 的依赖项

NestJS 错误:错误:Nest 无法解析 AuthService (?, JwtService) 的依赖项

Nest 在测试期间无法解析 UserService 的依赖关系

Nest.js 无法解决对 TestingModule 的循环依赖

Nest 无法解析 PhotoService 的依赖关系(?)