mongoose Nest无法解析UserModel(?)

yws3nbqq  于 11个月前  发布在  Go
关注(0)|答案(3)|浏览(91)

当我尝试在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 {}

czq61nw1

czq61nw11#

与其在database.provider.ts中静态配置mongoose,不如使用MongooseModule.forRootAsync导入:

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

字符串

im9ewurl

im9ewurl2#

异步传递模块选项,而不是静态传递。
您可以使用MongooseModule.forRootAsync。在app.module.ts中进行更改可以正常工作。

import { MongooseModule } from '@nestjs/mongoose';

@Module({
  imports: [
    MongooseModule.forRootAsync({
      useFactory: () => ({
        uri: 'mongodb://xxxxx',
      }),
    }),
   
  ],
})
export class AppModule {}

字符串

wj8zmpe1

wj8zmpe13#

在我的例子中,* main.ts * 错误地调用了NestFactory.create(UsersModule)而不是NestFactory.create(AppModule)

相关问题