简体   繁体   English

在nodejs中返回响应时,无法在将标头发送到客户端后设置标头

[英]Cannot set headers after they are sent to the client when return the response in nodejs

when i send a request to this function it show me this error:当我向此 function 发送请求时,它显示此错误:

    events.js:292
      throw er; // Unhandled 'error' event
      ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:533:11)
    at ServerResponse.header (F:\Projects\Nodejs\SalesSignal\node_modules\express\lib\response.js:771:10)
    at ServerResponse.send (F:\Projects\Nodejs\SalesSignal\node_modules\express\lib\response.js:170:12)
    at ServerResponse.json (F:\Projects\Nodejs\SalesSignal\node_modules\express\lib\response.js:267:15)
    at ServerResponse.send (F:\Projects\Nodejs\SalesSignal\node_modules\express\lib\response.js:158:21)
    at ManagerController.Ok (F:\Projects\Nodejs\SalesSignal\src\http\controller\BaseController.js:18:28)
    at F:\Projects\Nodejs\SalesSignal\src\http\controller\ManagerController.js:121:25

my function is this and this function fir update record:我的 function 是这个和这个 function 杉木更新记录:

      async DeleteManager(req, res, next) {
    let result = await this.ValidationAction(req, res);
    if (result[0]) {
      const user = Manager.findByIdAndUpdate(
        req.params.id,
        { $set: { isDelete: true } },
        { useFindAndModify: false },
        (error, user) => {
          if (error) {
            next(error);
          } else if (!user) {
            return  res.status(200).send({
              message: "رکورد مورد نظر یافت نشد",
              statusCode: 200,
              success: false,
            });
          } else {
            return res.status(200).send({
              message: "عملیات با موفقیت انجام شد",
              statusCode: 200,
              success: true,
            });
          }
        }
      );
    }
    return this.BadRerquest(res, result[1]);
  }

when i send a request to this functin it update record in database but it show me this error.当我向这个函数发送请求时,它会更新数据库中的记录,但它显示了这个错误。

whats the problem?有什么问题? how can i solve thus problem?我该如何解决这个问题?

Most likely your app tries to send multiple responses to the single request.您的应用很可能会尝试对单个请求发送多个响应。 This may happen because the app seems to always send BadRequest even if there is result[0] .这可能是因为应用程序似乎总是发送BadRequest ,即使有result[0] Try to change the control flow from:尝试从以下位置更改控制流:

if (result[0]) {
  // ...
}
return this.BadRerquest(res, result[1]);

to

if (result[0]) {
  // ...
} else {
  return this.BadRerquest(res, result[1]);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NodeJS-将标头发送到客户端后无法设置标头 - NodeJS - Cannot set Headers after they are sent to the client {NodeJS} "发送到客户端 NodeJS 后无法设置标头" - {NodeJS} "Cannot set headers after they are sent to the client NodeJS" http POST 响应后,发送到客户端后无法设置标头 - After http POST response, cannot set headers after they are sent to the client 使用nodejs mysql将标头发送到客户端后无法设置标头 - Cannot set headers after they are sent to the client using nodejs mysql Nodejs代理服务器发送到客户端后无法设置标头 - Nodejs proxy server Cannot set headers after they are sent to the client 将标头发送到客户端错误后无法设置标头 nodejs 错误 - Cannot set headers after they are sent to the client error nodejs error Express - NodeJS - 错误:将标题发送到客户端后无法设置标题 - Express - NodeJS - Error: Cannot set headers after they are sent to the client 当不喜欢记录我的nodejs时,无法在将标头发送到客户端后设置标头 - Cannot set headers after they are sent to the client when not fond the record i nodejs NodeJS SQL 登录错误(ERR_HTTP_HEADERS_SENT]:发送到客户端后无法设置标头) - NodeJS SQL Login Error (ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client) 错误 [ERR_HTTP HEADERS_SENT]:在将标头发送到 nodejs 的客户端后无法设置标头 - Error [ERR_HTTP HEADERS_SENT]: Cannot set headers after they are sent to the client at nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM