I'm currently trying to learn NestJS and came across this error while throwing an exception inside a middleware. Why does this happen?
Code:
import { HttpException, HttpStatus, Injectable, NestMiddleware } from '@nestjs/common';
import { IncomingMessage, ServerResponse } from 'http';
@Injectable()
export class ExampleMiddleware implements NestMiddleware {
use(req: IncomingMessage, res: ServerResponse, next: Function) {
const { authorization } = req.headers;
if (!authorization) throw new HttpException('No Authorization Token', HttpStatus.FORBIDDEN);
else if (authorization === 'password') next();
else throw new HttpException('Incorrect Authorization Token', HttpStatus.FORBIDDEN);
}
}
Error:
/home/api/node_modules/fastify/lib/reply.js:479
if (reply[kRouteContext].preSerialization !== null) {
^
TypeError: Cannot read properties of undefined (reading 'preSerialization')```