你将如何停止在NodeJs中执行代码的其余部分(而不是用process.exit()声明应用程序只是说这是要执行的最后一行)。如何停止执行NodeJs代码的其余部分?
在正则JavaScript中你有退出(),但似乎并没有在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
}
要停止执行一段代码,请使用break; –
把你想要避免执行的代码放到else块中。 –
您确定要退出而不调用'connection.end()'? –