So I'm getting an error like this
[Nest] 542997 - 05/03/2023, 10:24:09 PM ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'message')
TypeError: Cannot read properties of undefined (reading 'message')
at chatbot-backend/src/common/interceptor/response.interceptor.ts:38:22
at chatbot-backend/node_modules/rxjs/src/internal/operators/map.ts:58:33
at OperatorSubscriber._this._next (chatbot-backend/node_modules/rxjs/src/internal/operators/OperatorSubscriber.ts:70:13)
With the code in response.interceptor.ts like this
...
export interface Response<T> {
status: string;
statusCode: number;
author: string;
apiVersion: number;
message: string;
data: T;
}
@Injectable()
export class ResponseInterceptor<T> implements NestInterceptor<T, Response<T>> {
intercept(
context: ExecutionContext,
next: CallHandler,
): Observable<Response<T>> {
const request = context.switchToHttp().getRequest();
const version = +request.url.match(/\/v[0-9]/)?.[0].slice(2);
const response = context.switchToHttp().getResponse();
return next.handle().pipe(
map((res) => ({
status: [200, 201, 204].includes(response.statusCode)
? 'success'
: 'error',
statusCode: response.statusCode,
author: APP_AUTHOR,
apiVersion: isNaN(version) ? null : version,
message: res.message ?? null,
data: res.data ?? null,
})),
);
}
}
This is all in a normal controller but when you get a controller like this
async create(
@Res({ passthrough: true }) res: Response,
) {}
an error will occur, I am forced to do this because I use the baileys library in NestJS.
Can anyone help with the problem where the service will throw a response to the controller?