I have updated my application from Nest.js v8 to Nest.js v9 and now the application exits every time an error is thrown.
Previously, an HTTPException was simply returned. But now every time the server terminates and I get this error:
`TypeError: Cannot read properties of undefined (reading 'preSerialization')`
System:
Node: 18
The following packages have been updated:
@nestjs/common: 9.2.1
@nestjs/core: 9.2.1
@nestjs/microservices: 9.2.1
@nestjs/platform-express: 9.2.1
@nestjs/platform-fastify: 9.2.1
@nestjs/platform-socket.io: 9.2.1
@nestjs/swagger: 9.2.1
@nestjs/websockets: 9.2.1
The error only occurs when I throw an exception in the middleware, otherwise it works.
@Injectable()
export class AuthMiddleware implements NestMiddleware {
async use(req: Request, res: Response, next: NextFunction): Promise<NextFunction> {
if (req.method === 'OPTIONS') {
next();
}
if (!req.headers.authorization) {
throw new HttpException('No credentials set', HttpStatus.UNAUTHORIZED);
}
const token = req.headers.authorization;
const claims = await authClient.verify(token);
....
next();
}
}
Has anyone ever had the problem? I think it has something to do with fastify, but haven't found a solution yet.
I have also tried other versions but from v9 it does not work. It does also not work with other node versions.