【问题标题】:Express promise chain how to send response and end promise chain flow?Express Promise 链如何发送响应和结束 Promise 链流?
【发布时间】:2019-05-05 07:40:54
【问题描述】:

使用下面的代码我得到了这个错误:

Unhandled rejection Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client 如果result === null

'use strict'

const HttpStatus = require('http-status-codes')
const { handleErr } = require('ciitizen-express-helpers').utils

function updateOrganization(db, stats) {
  return function (req, res) {
    db.organization.findOne({
      where: {
        id: req.params.id
      }
    })
      .then(result => {

        if (result === null) {
          return res.status(HttpStatus.NOT_FOUND).send() <-- IF RESULT IS NULL, I RETURN THIS 
        }

        console.log('original result = ', result)

        // Update any fields that were passed in
        if (req.body.name) {
          result.name = req.body.name
        }

        if (req.body.address1) {
          result.address1 = req.body.address1
        }

        if (req.body.address2) {
          result.address2 = req.body.address2
        }

        if (req.body.city) {
          result.city = req.body.city
        }

        if (req.body.state) {
          result.state = req.body.state
        }

        if (req.body.zip) {
          result.zip = req.body.zip
        }

        console.log('new result = ', result)
        return result.save()
      })
      .then(result => {
        console.log('final result = ', result)
        return res.status(HttpStatus.CREATED).send(result) <-- BUT IT'S STILL TRYING TO CALL THIS, HENCE THE CAN'T SEND HEADERS ERROR
      })
      .catch(err => {
        req.log.error(err)
        return handleErr(res, HttpStatus.INTERNAL_SERVER_ERROR, err.message)
      })
  }
}

module.exports = updateOrganization

在不继续我的承诺链流程的情况下尽早返回响应的最佳方式是什么?

【问题讨论】:

  • IF RESULT IS NULL, I RETURN THIS ... BUT IT'S STILL TRYING TO CALL THIS 当然是...这就是承诺链的工作原理...我建议在res.status(HttpStatus.NOT_FOUND)之后返回null ...并在@987654328 @检查结果是否为空,如果是,跳过res.status(...代码

标签: javascript node.js express promise


【解决方案1】:

这样的承诺链

你能做的是

A) return null AFTER res.status(HttpStatus.NOT_FOUND)(删除该代码之前的 return

B) 在then(result 中检查结果是否为空,如果是,则跳过res.status(... 代码

例如

'use strict'

const HttpStatus = require('http-status-codes')
const { handleErr } = require('ciitizen-express-helpers').utils

function updateOrganization(db, stats) {
  return function (req, res) {
    db.organization.findOne({
      where: {
        id: req.params.id
      }
    })
      .then(result => {

        if (result === null) {
          // change A
          res.status(HttpStatus.NOT_FOUND).send();
          return null;
        }
        // snip
        return result.save()
      })
      .then(result => {
        console.log('final result = ', result)
        // change B
        if (result !== null) {
            return res.status(HttpStatus.CREATED).send(result)
        }
      })
      .catch(err => {
        req.log.error(err)
        return handleErr(res, HttpStatus.INTERNAL_SERVER_ERROR, err.message)
      })
  }
}

module.exports = updateOrganization

或者,保留现有代码直到

      .then(result => {
        // change C
        if (!res.headersSent) {
            console.log('final result = ', result)
            return res.status(HttpStatus.CREATED).send(result)
        }
      })

说实话,这可能是一个“更干净”的修复

【讨论】:

  • 我建议在 ` if (result !== null) { return res.status(HttpStatus.CREATED).send(result) }` 之后返回一些响应,这样端点就不会挂起和超时。
  • 它不会挂起@Catfish,因为这不是 javascript 的工作方式
  • 哦,等一下,nvm,我知道你打电话给res.status().send()
【解决方案2】:

测试一下:

    function updateOrganization(db, stats) {
  return function (req, res) {
    db.organization.findOne({
      where: {
        id: req.params.id
      }
    })
      .then(result => {

        if (!result) {
          res.status(HttpStatus.NOT_FOUND).end();
          return null;
        }

        console.log('original result = ', result)

        // Update any fields that were passed in
        if (req.body.name) {
          result.name = req.body.name
        }

        if (req.body.address1) {
          result.address1 = req.body.address1
        }

        if (req.body.address2) {
          result.address2 = req.body.address2
        }

        if (req.body.city) {
          result.city = req.body.city;
        }

        if (req.body.state) {
          result.state = req.body.state;
        }

        if (req.body.zip) {
          result.zip = req.body.zip;
        }

        console.log('new result = ', result);
        return result.save();
      })
      .then(result => {
        console.log('final result = ', result);
        if(result) {
            return res.status(HttpStatus.CREATED).json(result)
        }
        return null;

      })
      .catch(err => {
        req.log.error(err);
        return handleErr(res, HttpStatus.INTERNAL_SERVER_ERROR, err.message);
      })
  }
}

【讨论】:

    猜你喜欢
    • 2016-03-10
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 2016-02-25
    • 2015-01-19
    • 2020-07-30
    相关资源
    最近更新 更多