我正在用express js编写API,有一些条件,如果匹配,我会尝试回复数据。所以问题是如果两个条件匹配,我的代码响应两次,发送一次后我可以停止响应另一个条件吗?由于我来自PHP背景,因此该节点js的逻辑确实有所不同。
我仍然尝试使用res.end(),但是如果另一个条件匹配,它就会继续响应。如果条件类型不同,我就不能简单地编写else条件语句。
请看一下我的代码,如果可以帮助我解决此问题,非常感谢。
exports.createBooking = (req, res) => {
// Save Order to Database
console.log("Processing func -> Create Booking");
// console.log(req.body);
const business_code = 'Test' + new Date("03/25/2015") + Math.floor(Math.random() * 9999999999999) + 1 ;
const locker_station_id = req.body.locker_station_id || '';
const reference_id = req.body.reference_id || '';
console.log(locker_station_id);
const locker = db.sequelize.query(`SELECT * FROM ems_device where locker_station_id="${locker_station_id}"`,{ plain: true,type: sequelize.QueryTypes.SELECT})
.then(locker => { // data is equal to the result of line 1.
// console.log(locker);
if(locker){
// console.log(locker);
if(locker.status == 0){
res.status(422).json(ResponseFormat.error(
'locker_station_offline',
reference_id,
422
))
}
else if(locker.status == 2){
res.status(422).json(ResponseFormat.error(
'locker_station_retired',
reference_id,
422
))
}
getLockerboxCategory(req, res);
const locker_box_category = req.locker_box_category;
const drawer_resource_info = db.sequelize.query(`SELECT * FROM ems_drawer_resource_info where device_number="${locker.device_number}" and drawer_status=0`,{ type: sequelize.QueryTypes.SELECT})
.then(drawer_resource_info => { // data is equal to the result of line 1.
if(drawer_resource_info.length == 0){
res.status(422).json(ResponseFormat.error(
'insufficient_capacity',
reference_id,
422
))
}
});
}
});
}
function getLockerboxCategory(req, res){
// console.log(req.body.parcel_length);
const parcel_length = req.body.parcel_length || 0;
const parcel_height = req.body.parcel_height || 0;
const parcel_width = req.body.parcel_width || 0;
const parcel_weight = req.body.parcel_weight || 0;
const small_box_length = 43;
const small_box_height = 8;
const small_box_width = 47;
const medium_box_length = 43;
const medium_box_height = 19;
const medium_box_width = 47;
const large_box_length = 43;
const large_box_height = 28;
const large_box_width = 47;
if(parcel_height < small_box_height && parcel_width < small_box_width && parcel_length < small_box_length){
// small box
req.locker_box_category = 3;
req.locker_box_cost = 1;
}
else if(parcel_height < medium_box_height && parcel_width < medium_box_width && parcel_length < medium_box_length )
{
//medium box
req.locker_box_category = 2;
req.locker_box_cost = 1.5;
}
else if(parcel_height < large_box_height && parcel_width < large_box_width && parcel_length < large_box_length )
{
//large box
req.locker_box_category = 1;
req.ocker_box_cost = 2;
}else{
res.status(422).json(ResponseFormat.error(
'parcel_is_too_large',
req.reference_id||'',
422
));
res.end();
}
答案 0 :(得分:1)
欢迎使用NodeJS异步环境。使用res.json
时,将写入响应,并且流将结束,使用res.end()
时不使用res.json
。
拥有所需的一切时,请使用res.json
。
根据您的代码,您可能在locker.
语句之后创建了else分支:
exports.createBooking = (req, res) => {
// Save Order to Database
console.log("Processing func -> Create Booking");
// console.log(req.body);
const business_code = 'Test' + new Date("03/25/2015") + Math.floor(Math.random() * 9999999999999) + 1;
const locker_station_id = req.body.locker_station_id || '';
const reference_id = req.body.reference_id || '';
console.log(locker_station_id);
const locker = db.sequelize.query(`SELECT * FROM ems_device where locker_station_id="${locker_station_id}"`, {
plain: true,
type: sequelize.QueryTypes.SELECT
})
.then(locker => { // data is equal to the result of line 1.
// console.log(locker);
if (locker) {
// console.log(locker);
if (locker.status == 0) {
res.status(422).json(ResponseFormat.error(
'locker_station_offline',
reference_id,
422
))
} else if (locker.status == 2) {
res.status(422).json(ResponseFormat.error(
'locker_station_retired',
reference_id,
422
))
} else {
let lockerBoxStatus = getLockerboxCategory(req, res); // you need to know the status of this method, go and check getLockerboxCategory
// the stream will output the JSON if you reach the else branch, but make no mistake, the javascript is not done, the following lines will be evaluated.
if (lockerBoxStatus) { // if this is 0, then you will not get a write error, because getLockerboxCategory will close the stream.
const locker_box_category = req.locker_box_category;
const drawer_resource_info = db.sequelize.query(`SELECT * FROM ems_drawer_resource_info where device_number="${locker.device_number}" and drawer_status=0`, {
type: sequelize.QueryTypes.SELECT
})
.then(drawer_resource_info => { // data is equal to the result of line 1.
if (drawer_resource_info.length == 0) {
res.status(422).json(ResponseFormat.error(
'insufficient_capacity',
reference_id,
422
))
}
});
}
}
}
});
}
function getLockerboxCategory(req, res) {
// console.log(req.body.parcel_length);
let status = 1; // assuming 1 by default
const parcel_length = req.body.parcel_length || 0;
const parcel_height = req.body.parcel_height || 0;
const parcel_width = req.body.parcel_width || 0;
const parcel_weight = req.body.parcel_weight || 0;
const small_box_length = 43;
const small_box_height = 8;
const small_box_width = 47;
const medium_box_length = 43;
const medium_box_height = 19;
const medium_box_width = 47;
const large_box_length = 43;
const large_box_height = 28;
const large_box_width = 47;
if (parcel_height < small_box_height && parcel_width < small_box_width && parcel_length < small_box_length) {
// small box
req.locker_box_category = 3;
req.locker_box_cost = 1;
} else if (parcel_height < medium_box_height && parcel_width < medium_box_width && parcel_length < medium_box_length) {
//medium box
req.locker_box_category = 2;
req.locker_box_cost = 1.5;
} else if (parcel_height < large_box_height && parcel_width < large_box_width && parcel_length < large_box_length) {
//large box
req.locker_box_category = 1;
req.ocker_box_cost = 2;
} else {
status = 0;
res.status(422).json(ResponseFormat.error(
'parcel_is_too_large',
req.reference_id || '',
422
));
//res.end(); // you don't need this `.json` will output+end the stream
}
return status;
}
PS:请确保您所有的语句都被res.json
或res.end()
覆盖,您认为这适合终止流,否则Web服务器将挂起。并从中间件next
中了解app.use((req, res, next)=> next(); )
参数,当您想跳到下一个中间件时,可能需要它。
答案 1 :(得分:0)
如果不想深入了解您的代码ID,建议使用switch语句。一旦满足条件,它将允许您break;
退出语句。它们应该像在PHP中一样工作,但是这里是文档,以防万一https://www.w3schools.com/js/js_switch.asp。如果开关不起作用,则可能会在满足条件后退出该功能。