当我嵌套启动代码时,它会出现以下错误,问题是什么?
我将ConfigModule设置为Gloabl,因此不需要导入。
如果我错过任何密码,请告诉我,我可以在这里张贴。
我引用NestJS Jwt软件包中的用法
我以前在硬编码的秘密和选项中使用JwtModule.register(),它工作得很好。
[Nest] 159128 - 02/27/2021, 6:27:15 PM [ExceptionHandler] Nest cannot create the AuthModule instance.
The module at index [2] of the AuthModule "imports" array is undefined.
Potential causes:
- A circular dependency between modules. Use forwardRef() to avoid it. Read more: https://docs.nestjs.com/fundamentals/circular-dependency
- The module at index [2] is of type "undefined". Check your import statements and the type of the module.
TyprOrmModule可以很好地访问数据库,这证明了configService在.env中提取环境变量。
AppModule.ts:
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Connection } from 'typeorm';
import { join } from 'path';
import { UserModule } from './user/user.module';
import { AuthModule } from './auth/auth.module';
import { FormModule } from './form/form.module';
import { InventoryModule } from './inventory/inventory.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true
//, ignoreEnvFile: true
}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
type: 'mysql',
host: configService.get('DB_HOST'),
port: configService.get<number>('DB_PORT'),
username: configService.get('DB_USERNAME'),
password: configService.get('DB_PASSWORD'),
database: configService.get('DB_DATABASE'),
synchronize: true,
autoLoadEntities: true,
}),
inject: [ConfigService],
})
, UserModule, AuthModule, FormModule, InventoryModule,
],
controllers: [AppController],
})
export class AppModule {
constructor(private connection: Connection) { }
}
如果我添加imports ConfigModule并注入ConfigService,它会在nest开始时给出准确的错误。我只是不知道问题出在哪里。AuthModule.ts:
import { Module } from '@nestjs/common';
import { AuthController } from './auth.controller';
import { LdapStrategy } from './ldap/ldap.strategy';
import { AuthService } from './auth.service';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { jwtConstants } from './jwt/constants';
import { JwtStrategy } from './jwt/jwt.strategy';
import { UserModule } from 'src/user/user.module';
import { JwtRefreshTokenStrategy } from './jwt/jwt.refresh.strategy';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
PassportModule,
//ConfigModule,
JwtModule.registerAsync({
//imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
secret: configService.get<string>('JWT_SECRET'),
// signOptions: {
// algorithm: 'HS256',
// expiresIn: configService.get<number>('JWT_EXPIRES_IN_SEC'),
// }
}),
//inject: [ConfigService],
}),
, UserModule
],
controllers: [AuthController],
providers: [
LdapStrategy
, AuthService
, JwtStrategy
, JwtRefreshTokenStrategy
],
//exports: [AuthService],
})
export class AuthModule { }
发布于 2022-02-23 04:41:54
虽然这个答案已经很晚了,但我遇到了同样的问题,并在@nestjs/jwt
(https://github.com/nestjs/jwt#async-options)的Github站点上找到了解决方案。张贴在这里以防其他人碰到它。
看来,您还需要注入ConfigService
。
JwtModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
secret: configService.get<string>('SECRET'),
}),
inject: [ConfigService],
})
https://stackoverflow.com/questions/66398326
复制相似问题