首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NestJS -无法解析ConfigService

NestJS -无法解析ConfigService
EN

Stack Overflow用户
提问于 2020-02-17 11:41:25
回答 2查看 1.9K关注 0票数 2

我使用NestJS jwt passport对用户进行身份验证。我遵循文档,这是我的应用程序模块:

代码语言:javascript
运行
复制
import { Module } from '@nestjs/common';
import { UserModule } from './user/user.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from './auth/auth.module';
import { ConfigModule } from '@nestjs/config';
import configuration from '../config/configuration';

@Module({
  imports: [
      ConfigModule.forRoot({
          load: [configuration],
      }),
      TypeOrmModule.forRoot(),
      UserModule,
      AuthModule,
  ],
})
export class AppModule {}

下面是我的auth模块:

代码语言:javascript
运行
复制
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { PassportModule } from '@nestjs/passport';
import { UserModule } from '../user/user.module';
import { LocalStrategy } from './local.strategy';
import { JwtStrategy } from './jwt.strategy';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import { AuthController } from './auth.controller';

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

每次运行npm run start时,我都会收到以下错误消息:

代码语言:javascript
运行
复制
Nest can't resolve dependencies of the JWT_MODULE_OPTIONS (?). Please make sure that the argument ConfigService at index [0] is available in the JwtModule context.

我已经搜索了这个问题,但仍然不能解决这个问题。有人能帮我吗?谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-18 17:16:25

正如其他人提到的,一种选择是使用isGlobal: true设置使ConfigModule成为全局的。否则,在@nestjs/config@0.2.3中使用should be fixed,现在应该可以像文档显示的那样工作。

票数 2
EN

Stack Overflow用户

发布于 2020-02-18 11:54:24

我与这个问题斗争了几个小时,但据Chau Tran说,在用ConfigModule.forRoot({ isGlobal: true })使ConfigModule全球化后,我所有的问题都解决了。?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60255641

复制
相关文章

相似问题

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