【问题标题】:Nestjs crashes when throwing error after update to v9更新到 v9 后抛出错误时 Nestjs 崩溃
【发布时间】:2023-01-03 02:45:47
【问题描述】:

我已将我的应用程序从 Nest.js v8 更新到 Nest.js v9,现在每次抛出错误时应用程序都会退出。

以前,只是返回 HTTPException。但是现在每次服务器终止时我都会收到此错误:

`TypeError: Cannot read properties of undefined (reading 'preSerialization')`

系统:

节点:18

以下软件包已更新:

@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

该错误仅在我在中间件中抛出异常时发生,否则它会起作用。

@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();
    }
}

有没有人遇到过这个问题?我觉得跟fastify有关系,但是还没找到解决方法。

我也尝试过其他版本,但从 v9 开始它不起作用。它也不适用于其他节点版本。

【问题讨论】:

  • 你设法解决这个问题了吗?升级到v9后有同样的问题
  • 不,不幸的是没有

标签: node.js nestjs nestjs-fastify


【解决方案1】:

似乎这种变化发生在大于9.1.2的版本中,我在https://github.com/nestjs/nest/issues/10781上报告了同样的问题 目前还没有针对这个问题的实际修复,我的临时解决方案是使用 9.1.2(这个 bug 从 nestjs 9.1.3 开始)

【讨论】:

    猜你喜欢
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 2020-11-07
    • 2015-01-04
    • 1970-01-01
    • 2016-09-11
    相关资源
    最近更新 更多