简体   繁体   English

如何停止执行NodeJ的其余代码?

[英]How to stop rest of code from executing NodeJs?

How would you stop the rest of the code from executing in NodeJs (not termine the application with process.exit() just say this is the last line to be executed). 您如何停止其余代码在NodeJ中执行(不是用process.exit()终止应用程序,只是说这是要执行的最后一行)。

In regular javascript you have exit(), but that doesnt seem to work in nodejs. 在常规javascript中,您具有exit(),但是在nodejs中似乎不起作用。

Below is the situation: 情况如下:

 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'


      });



    });

Essentially when this condition is met, I would like the rest of the code not to be executed 本质上,当满足此条件时,我希望其余代码不执行

if (!userFound) {
        res.render('pickup/errorAlreadyDelivered', {});
        // exit here 
}

connection.end(); then return; 然后return; seems like it might work for you. 似乎它可能为您工作。

So based on your comment on my other answer, it sounds like your program structure isn't what I thought it was. 因此,根据您对我的其他答案的评论,听起来您的程序结构不是我想的那样。 This should work: 这应该工作:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM