[英]How to stop rest of code from executing NodeJs?
您如何停止其余代碼在NodeJ中執行(不是用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
}
connection.end();
然后return;
似乎它可能為您工作。
因此,根據您對我的其他答案的評論,聽起來您的程序結構不是我想的那樣。 這應該工作:
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.