简体   繁体   English

Nest.js 无法解析 CacheConfigService 的依赖

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

I want to use the CacheModule in the AuthModule and I have written this code but still getting the error in the console:我想在 AuthModule 中使用 CacheModule 并且我已经编写了这段代码但仍然在控制台中收到错误: 在此处输入图像描述

The Auth.module.ts file in which I want to import the cache module:我要在其中导入缓存模块的 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],
    };
  }
}

The cache config.module.ts file code.缓存config.module.ts 文件代码。 The cache config.service.ts file contains the logic:缓存 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 {}

I want to use the cache service in the following class:我想使用下面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;
                },
              };
            },
          },
        }),
      ],
    });
  }
}

The error has been thrown because you have used ConfigService in CacheConfigService , but it has never been imported into CacheConfigModule .报错是因为你在CacheConfigService中使用了ConfigService ,但从未导入到CacheConfigModule中。

If you want to have ConfigService then you must import it into AppModule , as well as CacheConfigModule .如果你想拥有ConfigService那么你必须将它导入AppModule ,以及CacheConfigModule

app.module.ts :应用程序模块.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 {}

config.module.ts :配置.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