title: node js check if data exists in mysql table.- TypeError: res.send is not a function
body: <p>I am trying to check if data exists in mysql table. I get an error <strong>TypeError: res.send is not a function</strong>.</p>
  
  <p>My mysql table has a lot of data but 3 columns with all varchar(45) and id (autoincremental) which is -</p>
  
  <pre><code>**id | metrics | custom_metrics**
    1  | test    | test
  </code></pre>
  
  <p>my <strong>js</strong> file inside routes - </p>
  
  <pre><code>    router.post('/addcustom',function(req,res){
          try {
              /*if (req.body != null) {*/
                  var customMetric = req.body.customMetric;
                  //var metrics = req.body.metrics;
                  var comma = ',';
                  console.log("***CUSTOMMETRIC1: "+JSON.stringify(customMetric) +"comma: "+comma);
                  // check if data exists in table start
                  connection.query('select custom_metrics from metrics_list where custom_metrics = ?',[customMetric], function(err, result, response) {
                       if (err) {
                          console.log('Count Error :' + err);
                      } else {
                          console.log("METRICS WHERE FOUND: "+JSON.stringify(result)+"****req.body.customMetric== "+customMetric);
                          var res = JSON.stringify(result);
  
                          if(result!=''||result!=null){
  
                             res.send('yes');
  
                             return false;
  
                          } else {
                              console.log("NOT FOUND");
                          }
                      }
                  }); //select custom metrics validation check query
  
              //check if data exists in table end
              /*connection.query('INSERT INTO metrics_list (customMetric,metrics) values ("' + customMetric + '"' +comma +'"' + metrics +'")', function(err, result) {*/
                  connection.query('SELECT COUNT(DISTINCT custom_metrics) FROM metrics_list;', function(err, result) {
                  if (err) {
                      console.log('Count Error :' + err);
                  } else {
                      console.log('Metrics Successfully Counted:' + JSON.stringify(result));
                     // console.log("CUSTOMMETRIC HTML req: "+customMetric);
               // connection query nesting 1start
                     var id1 = JSON.stringify(result).replace(/\D/g,'');
                     console.log("ID1: "+id1);
                      connection.query('UPDATE metrics_list SET custom_metrics = ? WHERE id = ?', [customMetric,id1], function(err, result1) {
                      if (err) {
                        console.log('Count Error :' + err);
                      } else {
                         console.log(" Update Successfully "+JSON.stringify(result1));
                        //res.redirect('/metrics/add');
                     }
                      });
  
                  }
              });
          /*}*/
      } catch (ex) {
          console.log("Exception : " + ex);
      } 
  })
  </code></pre>
  
  <p>My ajax call in the html page is-</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(document).ready(function() {
  
          $('#addbtn').on('click',function(){
              var customMetric = $('#cus_metrics').val();
              var dataString = 'customMetric='+customMetric;
              //alert(dataString);
              var letterNumber = /^[0-9a-zA-Z]+$/; 
              if(customMetric =='') {        
               alert("This field cannot be empty");
              }// if null ends
              else if(customMetric.length &lt;= 3 || customMetric.length &gt;= 20){
                  alert("This field must contain atleaset more than 3 and less than 20 characters");
                  return false;
              }
              else {
              $.ajax({
                  method:'POST',
                  url:'/metrics/addcustom',
                  data:dataString,
                  success:function(response){     
                  if (response == 'no') {
                              //alert("success");
                             /* $("#success-alert").css("display", "block");
                              top.location.href = "/dashboard";*/
                              alert("GOT A NO");
                          }             
                          else{
                              alert("got a yes");
                          }    
  
                  }
              }); //$.ajax ends
  }
          });
  &lt;/script&gt;
  </code></pre>
  
  <p>My HTML button points to the js file, I am only adding the button html since I need help only for the ajax part and the save and list works perfectly</p>
  
  <pre><code>&lt;div class="form-group"&gt;
                                          &lt;label class="col-sm-2 control-label"&gt;Add New Custom Metrics&lt;/label&gt;
                                          &lt;div class="col-lg-3"&gt;
                                              &lt;input type="text" id="cus_metrics" name="cus_metrics" class="form-control" placeholder="" /&gt;
                                          &lt;/div&gt;
                                          &lt;div class="col-lg-3"&gt;
                                  &lt;div style="text-align: left;width:100%;height:100%;"&gt;
                                      &lt;button type="button" id="addbtn" style="margin-left: 28%;width: 28%;" class="btn btn-primary"&gt;Add&lt;/button&gt;
                                  &lt;/div&gt;                                            
                                          &lt;/div&gt;                                        
                                  &lt;/div&gt; 
  </code></pre>
  
  <p>According to my ajax code , I am sending parameters ID to the JS file and as the query suggests, I am trying to use a where clause <code>connection.query('select custom_metrics from metrics_list where custom_metrics = ?',[customMetric], function(err, result, response)</code>.</p>
  
  <p>I want to send a response to the HTML file, and get a POP up alert box if the file exists using the res.send method, which apparently is not working. Request you to help me with the perfect code since I am stuck with this for a day now!! </p>
  
  <p>HELP is appreciated... </p>