Sunday, 16 April 2017

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).

In regular javascript you have exit(), but that doesnt seem to work in 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
  var connectionUpdate = mysql.createConnection({
    host : '...',
    user : '...',
    password : '....',
    database : '...'
  });
[...]

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

  }



via John

No comments:

Post a Comment