首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NestJS JwtModule.registerAsync()在导入时给出未定义的

NestJS JwtModule.registerAsync()在导入时给出未定义的
EN

Stack Overflow用户
提问于 2021-02-27 11:26:42
回答 1查看 769关注 0票数 1

当我嵌套启动代码时,它会出现以下错误,问题是什么?

我将ConfigModule设置为Gloabl,因此不需要导入。

如果我错过任何密码,请告诉我,我可以在这里张贴。

我引用NestJS Jwt软件包中的用法

我以前在硬编码的秘密和选项中使用JwtModule.register(),它工作得很好。

代码语言:javascript
运行
复制
[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:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
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 { }

EN

回答 1

Stack Overflow用户

发布于 2022-02-23 04:41:54

虽然这个答案已经很晚了,但我遇到了同样的问题,并在@nestjs/jwt (https://github.com/nestjs/jwt#async-options)的Github站点上找到了解决方案。张贴在这里以防其他人碰到它。

看来,您还需要注入ConfigService

代码语言:javascript
运行
复制
JwtModule.registerAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    secret: configService.get<string>('SECRET'),
  }),
  inject: [ConfigService],
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66398326

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档