簡體   English   中英

Nest.js 無法解析 CacheConfigService 的依賴

[英]Nest.js can't resolve the dependencies of CacheConfigService

我想在 AuthModule 中使用 CacheModule 並且我已經編寫了這段代碼但仍然在控制台中收到錯誤: 在此處輸入圖像描述

我要在其中導入緩存模塊的 Auth.module.ts 文件:

@Module({
  providers: [CacheConfigService, SupertokensService],
  exports: [],
  imports: [CacheConfigModule, UsersModule],
  controllers: [],
})
export class AuthModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(AuthMiddleware).forRoutes('*');
  }

  static forRoot({
    connectionURI,
    apiKey,
    appInfo,
  }: AuthModuleConfig): DynamicModule {
    return {
      // providers and exports
      imports: [CacheConfigModule],
    };
  }
}

緩存config.module.ts 文件代碼。 緩存 config.service.ts 文件包含邏輯:

@Module({
  providers: [CacheConfigService],
  exports: [CacheConfigService],
  imports: [
    CacheModule.register<RedisClientOptions>({
      isGlobal: true,
      // store: redisStore,
      url: 'redis://' + process.env.REDIS_HOST + ':' + process.env.REDIS_PORT,
    }),
  ],
})
export class CacheConfigModule {}

我想使用下面class中的緩存服務:

@Injectable()
export class SupertokensService {
  private redisClient = redis.createClient({
    url: this.cacheConfigService.url,
  });

  constructor(
    @Inject(forwardRef(() => UsersService)) private userService: UsersService,
    private cacheConfigService: CacheConfigService
  ) {
    supertokens.init({
      appInfo: this.config.appInfo,
      supertokens: {
        connectionURI: this.config.connectionURI,
        apiKey: this.config.apiKey,
      },
      recipeList: [
        ThirdPartyEmailPassword.init({
          providers: [
            ThirdPartyEmailPassword.Google({
              clientSecret: 'TODO: GOOGLE_CLIENT_SECRET',
              clientId:
                'CLIENT_ID',
            }),
          ],
          signUpFeature: {
            ...signup logic
          },
          override: {
            apis: (originalImplementation: any) => {
              return {
                ...originalImplementation,

                emailPasswordSignInPOST: async (input: any) => {
                  if (
                    originalImplementation.emailPasswordSignInPOST === undefined
                  ) {
                    throw Error('Should never come here');
                  }

                  let response: any =
                    await originalImplementation.emailPasswordSignInPOST(input);

                  // retrieving the input from body logic
                  const { email } = inputObject;
                  const user = await this.userService.findOneByEmail(email);
                  const id = user?.id;
                  const token = jwt.sign({email, id}, 'mysecret';, {
                    expiresIn: '2h',
                  });
                  response.token = token;
                  await this.redisClient.set("Token", token, {
                    EX: 60 * 60 * 24,
                  });
                  return response;
                },
              };
            },
          },
        }),
      ],
    });
  }
}

報錯是因為你在CacheConfigService中使用了ConfigService ,但從未導入到CacheConfigModule中。

如果你想擁有ConfigService那么你必須將它導入AppModule ,以及CacheConfigModule

應用程序模塊.ts

import { ConfigModule } from '@nestjs/config';
import configuration from './config/configuration'; // or wherever your config file exists

@Module({
  imports: [
    ConfigModule.forRoot({
      load: [configuration],
    }),
    // rest of code
  ],
  // rest of code
})
export class AppModule {}

配置.module.ts

import { ConfigModule } from '@nestjs/config';

@Module({
  providers: [CacheConfigService],
  exports: [CacheConfigService],
  imports: [
    CacheModule.register<RedisClientOptions>({
      isGlobal: true,
      // store: redisStore,
      url: 'redis://' + process.env.REDIS_HOST + ':' + process.env.REDIS_PORT,
    }),
    ConfigModule,
  ],
})
export class CacheConfigModule {}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM