0

So my first res.send command works but after making queries I cant get them to work. Not sure what to do. The console.log works like a charm for each if statement but the res.send is being really finnicky.

app.get('/info', function(req,res) {
var option1 = req.query.option1
var option2 = req.query.option2
var option3 = req.query.option3

if (option1 == 'SELECT' || option2 == 'SELECT' || option3 == 'SELECT') {
    res.send('Please select all valid fields and try again');
    console.log('Not all fields were filled out');
}
    
    
if (option3 == 'Cash') {
var queryCash = connection.query('SELECT toll FROM tollschedule.cashtollschedule WHERE interchangeEnter=' + option1 + ' AND interchangeExit=' + option2);
var formatCash = queryCash[0].toll;
var answerCash = '$' + parseFloat(formatCash).toFixed(2);
    res.send('Cash');
    console.log(queryCash);
    console.log('Toll request received from ' + option1 + ' to ' + option2 + ' costing ' + answerCash);
}

if (option3 == 'E-ZPass') {
var queryEz = connection.query('SELECT toll FROM tollschedule.ezpasstollschedule WHERE interchangeEnter=' + option1 + ' AND interchangeExit=' + option2);
var formatEz = queryEz[0].toll;
var answerEz = '$' + parseFloat(formatEz).toFixed(2);   
    res.send('EZ');
    console.log(queryEz);
    console.log('Toll request received from ' + option1 + ' to ' + option2 + ' costing ' + answerEz);
}
})

1 Answers1

0

This way of sending response back is wrong because your code under certain conditions will try to send multiple responses to the request(which will throw an error). For example, consider the case when the value of options is like this- option1='SELECT', option2='SELECT', option3='Cash' for these values two if statements will be executed and your code will try to send two responses which will give error.

In order to avoid this, refactor your code like this :

     app.get('/info', function(req,res) {
       var option1 = req.query.option1
       var option2 = req.query.option2
       var option3 = req.query.option3

       if (option1 == 'SELECT' || option2 == 'SELECT' || option3 == 'SELECT') {
           res.send('Please select all valid fields and try again');
           console.log('Not all fields were filled out');
       }
       else{
          if (option3 == 'Cash') {
            var queryCash = connection.query('SELECT toll FROM tollschedule.cashtollschedule WHERE interchangeEnter=' + option1 + ' AND interchangeExit=' + option2);
            var formatCash = queryCash[0].toll;
            var answerCash = '$' + parseFloat(formatCash).toFixed(2);
            res.send('Cash');
            console.log(queryCash);
            console.log('Toll request received from ' + option1 + ' to ' + option2 + ' costing ' + answerCash);
           }
           else if (option3 == 'E-ZPass') {
             var queryEz = connection.query('SELECT toll FROM tollschedule.ezpasstollschedule WHERE interchangeEnter=' + option1 + ' AND interchangeExit=' + option2);
             var formatEz = queryEz[0].toll;
             var answerEz = '$' + parseFloat(formatEz).toFixed(2);   
             res.send('EZ');
             console.log(queryEz);
             console.log('Toll request received from ' + option1 + ' to ' + option2 + ' costing ' + answerEz);
          }
        }
      })
Naveen Chahar
  • 561
  • 3
  • 10
  • Thank you! However the res.send commands will still not work besides the first one. Not sure what is going on here. – Jack Marshall Nov 15 '20 at 17:06
  • You can do res.send() only once for a request. If you want to send data multiple times then use res.write() and res.end() at last. – Naveen Chahar Nov 15 '20 at 17:10
  • check this out for more info : https://stackoverflow.com/questions/44692048/what-is-the-difference-between-res-send-and-res-write-in-express/44693016 – Naveen Chahar Nov 15 '20 at 17:11