Я бы назвал службу внутри перехватчика в NestJS ( см. Документ ), вот как я сделал
export class HttpInterceptor implements NestInterceptor {
constructor(private configService:ConfigService){}
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
let request = context.switchToHttp().getRequest();
const apikey= this.configService.get('apikey');
const hash=this.configService.get('hash');
request.params= {apikey:apikey ,hash:hash,ts:Date.now()}
return next
}
}
Hers's ConfigService
export class ConfigService {
private readonly envConfig: { [key: string]: string };
constructor(filePath: string) {
this.envConfig = dotenv.parse(fs.readFileSync(path.join(__dirname, filePath)));
}
get(key: string): string {
return this.envConfig[key];
}
}
Я получаю сообщение об ошибке, что configService не определен
Невозможно прочитать свойство 'get' из неопределенного
Но я инстанцировал ConfigService
правильно
Я не знаю, почему я не могу использовать ConfigService
внутри перехватчика