如何阻止其余的代码在NodeJs中执行(不是使用process.exit()来定义应用程序只是说这是要执行的最后一行。
在普通的javascript中你有exit(),但这似乎不适用于nodejs。
以下是情况:
connection.query(sql, [req.session.sessionUserPackagePassword] , function(err, rows, fields) {
if (!err) {
var userFound = false;
for(var i=0; i< rows.length; i++) {
// Make the comparaison case insensitive
if ((rows[i].deliveredToUser).toLowerCase() == `no`) {
userFound = true;
console.log(userFound);
[...]
}
}
if (!userFound) {
res.render('pickup/errorAlreadyDelivered', {});
// exit here
}
console.log(userFound);
// If the query fails to execute
} else {
console.log('Error while performing Query.');
res.render('errorConnection', {});
}
});
connection.end();
// Update the query to say the box has been delivered to user and specify time
// Establish the connection
[...]
res.render('pickup/openPackageClose', {
title: '',
helpButtonURL: '/help/help-dropPackage',
helpButtonTitle: 'Help'
});
});
基本上当满足这个条件时,我希望其余的代码不被执行
if (!userFound) {
res.render('pickup/errorAlreadyDelivered', {});
// exit here
}
答案 0 :(得分:1)
connection.end();
然后return;
似乎可能适合您。
答案 1 :(得分:1)
因此,基于您对我的其他答案的评论,听起来您的程序结构并非我认为的那样。这应该有效:
connection.query(sql, [req.session.sessionUserPackagePassword] , function(err, rows, fields) {
if (!err) {
var userFound = false;
for(var i=0; i< rows.length; i++) {
// Make the comparaison case insensitive
if ((rows[i].deliveredToUser).toLowerCase() == `no`) {
userFound = true;
console.log(userFound);
[...]
}
}
if (!userFound) {
res.render('pickup/errorAlreadyDelivered', {});
connection.end();
} else {
console.log(userFound);
console.log('Error while performing Query.');
res.render('errorConnection', {});
connection.end();
}