【发布时间】:2019-02-26 16:48:13
【问题描述】:
我正在尝试在我创建的以下异常处理程序中注入 nestjs-config:
import { ExceptionFilter, Catch, ArgumentsHost, Injectable } from '@nestjs/common';
import { HttpException } from '@nestjs/common';
import { InjectConfig } from 'nestjs-config';
@Injectable()
@Catch()
export class HttpExceptionFilter implements ExceptionFilter {
constructor(
@InjectConfig()
private readonly config,
) {
this.config = config.get('errors');
}
catch(exception: HttpException, host: ArgumentsHost) {
// some code here that calls this.config
}
}
但它返回未定义:TypeError: Cannot read property 'get' of undefined
这是全局定义异常处理程序的方式:
const app = await NestFactory.create(AppModule, { cors: true });
app.useGlobalFilters(new HttpExceptionFilter());
await app.listen(3000);
【问题讨论】:
-
您能添加您的 AppModule 代码吗?在我看来,ConfigService 在当前上下文中不可用。
-
一张便条——你不需要
@Injectable(),因为@Catch()已经在这里了
标签: javascript node.js typescript import nestjs