Я создал перехватчик, как показано ниже, который я буду использовать sh для глобального использования. Я добавил перехватчик в свой модуль и настроил его так, чтобы nest js обрабатывал DI для меня за Nest JS Docs , однако, когда я делаю запрос в свой сервис, я получаю ошибку показывая Cannot read property log of undefined
, поэтому кажется, что гнездо JS не заботится о DI.
Код перехватчика:
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { LoggingService } from './logging.service';
@Injectable()
export class AuthInterceptor implements NestInterceptor {
constructor(private readonly loggingService: LoggingService) { }
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next
.handle()
.pipe(
map((response) => {
this.loggingService.log('Responded successfully');
return response;
})
);
}
}
Модуль перехватчика:
import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { AuthInterceptor } from './auth.interceptor';
import { LoggingService } from './logging.service';
@Module({
providers: [
LoggingService,
{
provide: APP_INTERCEPTOR,
useClass: AuthInterceptor,
},
],
})
export class AuthInterceptorModule {}
Мой app.module.ts
в root моего приложения импортирует AuthInterceptorModule. Я предполагаю, что что-то напутал, но мне не ясно, как исправить эту проблему с DI.