Sunday, 16 April 2017

Issues retrieving a value to use for comparaison in an if statement

I have the following variable:

   var userFound = false;

In the event this turns true, I would like the below if statement to be executed:

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

  }

However, the issue is that it does not take into account the value change of the variable inside of a for loop above. Below is larger portion of the code.

   var userFound = false;

      let sql = `SELECT box_id, cubby_id, comport, deliveredToUser
               FROM recipients
               WHERE package_password = ?`;

        connection.query(sql, [req.session.sessionUserPackagePassword] , function(err, rows, fields) {
            if (!err) {
                for(var i=0; i< rows.length; i++) {
                    // Make the comparaison case insensitive

                                  if ((rows[i].deliveredToUser).toLowerCase() == `no`) {
                                  userFound = true;



                        var comport = rows[i].comport;
                        var command = "open" + rows[i].cubby_id;
                        var commandClose = "close" + rows[i].cubby_id;
                        var commandStatus = "status" + rows[i].cubby_id;



                        console.log(command);
                        console.log(comport);


                        var options = {
                            scriptPath: 'python/scripts',
                            args: [command, comport, commandClose, commandStatus] // pass arguments to the script here

                        };


                        PythonShell.run('controlLock.py', options, function (err, results) {
                if (err) {
                    res.render('errorConnection', {});
                } 
                        console.log('results: %j', results);
                        });

            }



    }

        // If the query fails to execute
        } else {
          console.log('Error while performing Query.');
            res.render('errorConnection', {});

        }
      });
      connection.end();

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

      }

Any help would be greatly appreciated.



via John

No comments:

Post a Comment