javascript - NodeJS - 将标头发送到客户端后无法设置标头
问题描述
所以我四处搜索,发现要解决上述问题,我必须在发送回复后返回。但我的问题是,即使我有回报,我仍然有错误。
const dbEditCourse = (req, res, db, logger) => {
let {
origCourse, code, description, type
} = req.body;
if (!code || !description || !type) {
res.json({
haveEmpty: true
});
return;
}
db.transaction((trx) => {
db.select('*').from('course_strand').where('code', '=', code)
.then(data => {
if (data[0]) {
//error happens in this block of code
res.json({
isSuccess: false
});
return;
//i also tried return res.json({ isSuccess: false });
}
//wrapping this in 'else' also does not work
return db('course_strand')
.returning('*')
.where('code', '=', origCourse)
.update({ code, description, type })
})
.then(course => {
return db('activity_logs')
.returning('*')
.insert({
date: new Date(),
employee_id: req.session.emp_id,
module: "COURSE / STRAND",
activity: "EDIT"
})
})
.then(activity => {
if (activity[0]) {
res.json({
isSuccess: true
});
return;
} else {
res.json({
isSuccess: false
});
return;
}
})
.then(trx.commit)
.catch(err => {
logger.error(err);
trx.rollback;
res.render('pages/error-500');
});
})
.catch(err => logger.error(err));
}
module.exports = {
dbEditCourse
}
我正在做的是产生错误,如果记录存在,它将进入上面的代码块。除了那个特定的代码块之外,我在其他地方没有遇到错误。即使我有错误,代码也能正常工作。
解决方案
您不能使用 return 关键字破坏承诺链,所有.then
语句都将被执行(不包括您在 a 中抛出错误.then
),res.json
已被多次调用。
处理 catch 块中的所有错误(包括您的错误和系统错误)。
在 catch 块中,检查错误是由您抛出还是不返回响应。
const dbEditCourse = (req, res, db, logger) => {
let {
origCourse, code, description, type
} = req.body;
if (!code || !description || !type) {
res.json({
haveEmpty: true
});
return;
}
// util throw a error
const breakWithMyError = () => {
throw new Error("MY_ERROR");
}
db.transaction((trx) => {
db.select('*').from('course_strand').where('code', '=', code)
.then(data => {
if (data[0]) {
//error happens in this block of code
breakWithMyError();
//i also tried return res.json({ isSuccess: false });
}
//wrapping this in 'else' also does not work
return db('course_strand')
.returning('*')
.where('code', '=', origCourse)
.update({ code, description, type })
})
.then(course => {
return db('activity_logs')
.returning('*')
.insert({
date: new Date(),
employee_id: req.session.emp_id,
module: "COURSE / STRAND",
activity: "EDIT"
})
})
.then(activity => {
// revert logic, we check for error case first
if (!activity[0]) {
breakWithMyError();
}
})
.then(trx.commit)
.then(() => {
// finally you can run to here without any error
res.json({
isSuccess: true
});
})
.catch(err => {
// If you any error, the error comes form `breakWithMyError` or any things.
if (err.message === "MY_ERROR") {
// the error throw by `breakWithMyError`
return res.json({
isSuccess: false
});
}
logger.error(err);
trx.rollback;
// Why you return a html page in failed case? `res.status(500).json({message: "Internal server!"});`
res.render('pages/error-500');
});
})
.catch(err => logger.error(err));
}
module.exports = {
dbEditCourse
}
推荐阅读
- swift - 增强的电子商务不向谷歌分析迅速报告数据
- c# - C# web api 条件检查是否使用相同的参数调用了相同的函数
- julia - 在 Julia 中使用 MathProgBase 的非线性约束最大化问题
- c# - Log4net - 将 LogicalThreadContext 添加到 .NET WebAPI 项目中的每个请求
- fortran - ITpp ld 找不到 -lfftw3 -llapack 和 -lblas
- libgdx - 如何修复 touchDragged 中的拖动移动?
- java - 通过底部导航视图的活动将值从片段传递到片段
- python - 按字符串名称拆分列表
- javascript - 我在 react-native-volume 实现中有错误
- sql - SQL查询计数不同表中相同id的行