首页 > 解决方案 > NestJS 和 Express 导致间歇性 UnhandledPromiseRejectionWarning

问题描述

我不能因为我的生活无法弄清楚这一点。它经常发生一次,不会导致应用程序本身出现任何问题,但我不知道是什么原因造成的。如果这与它有任何关系,我会从浏览器同时调用 api。

[Nest] 16608   - 11/7/2018, 1:23:08 PM   [NestApplication] Nest application successfully started
[Nest] 16608   - 11/7/2018, 1:23:23 PM   [LoggerMiddleware] Request /api/observation-statement/pending-count +14943ms
[Nest] 16608   - 11/7/2018, 1:23:23 PM   [LoggerMiddleware] Request /api/observation-statement/revision-count +16ms


(node:14968) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:469:11)
at ServerResponse.header (C:\Users\Ricardo Saracino\IdeaProjects\sor-api\node_modules\express\lib\response.js:767:10)
at ServerResponse.send (C:\Users\Ricardo Saracino\IdeaProjects\sor-api\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (C:\Users\Ricardo Saracino\IdeaProjects\sor-api\node_modules\express\lib\response.js:267:15)
at ExpressAdapter.reply (C:\Users\Ricardo Saracino\IdeaProjects\sor-api\node_modules\@nestjs\core\adapters\express-adapter.js:44:52)
at ExceptionsHandler.catch (C:\Users\Ricardo Saracino\IdeaProjects\sor-api\node_modules\@nestjs\core\exceptions\base-exception-filter.js:16:33)
at ExceptionsHandler.next (C:\Users\Ricardo Saracino\IdeaProjects\sor-api\node_modules\@nestjs\core\exceptions\exceptions-handler.js:15:20)
at C:\Users\Ricardo Saracino\IdeaProjects\sor-api\node_modules\@nestjs\core\router\router-proxy.js:23:35
at Layer.handle_error (C:\Users\Ricardo Saracino\IdeaProjects\sor-api\node_modules\express\lib\router\layer.js:71:5)
at trim_prefix (C:\Users\Ricardo Saracino\IdeaProjects\sor-api\node_modules\express\lib\router\index.js:315:13)
(node:14968) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14968) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我已经将调用减少到一个非常简单的函数,并确保调用返回一个唯一值(我认为这可能是 304 问题)

@Get('revision-count')
async countRevisions(@User() user, @Param() params) {

    user.count ++;

    return {
        status: 'success',
        data: {
            rand: user.count,
        }
    };
}

这是在中间件之前发生的

标签: javascriptnode.jstypescriptexpressnestjs

解决方案


推荐阅读