With the code below i'm getting this error:
Unhandled rejection Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
if 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
What's the best way to return a response early without continuing through my promise chain flow?
Promises chain like that
What you can do is
A) return null
AFTER res.status(HttpStatus.NOT_FOUND)
(removing the return
before that code)
B) in then(result
check if result is null, if so, skip the res.status(...
code
eg
'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
alternatively, keep your existing code until
.then(result => {
// change C
if (!res.headersSent) {
console.log('final result = ', result)
return res.status(HttpStatus.CREATED).send(result)
}
})
That's probably a "cleaner" fix to be honest
Test this:
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);
})
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.