2016-10-02 44 views
0

我想检查数据是否存在于mysql表中。我收到一个错误TypeError:res.send不是函数节点js检查数据是否存在于mysql表中。 - TypeError:res.send不是函数

我的MySQL表有大量的数据,但所有VARCHAR(45)和ID(自动增加),这是3列 -

**id | metrics | custom_metrics** 
    1 | test | test 

JS文件中的路线 -

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); 
    } 
}) 

我的阿贾克斯在html页面调用是 -

<script type="text/javascript"> 
    $(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 <= 3 || customMetric.length >= 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 
} 
     }); 
</script> 

M ŸHTML按钮指向js文件,我只是将按钮的HTML,因为我只需要帮助阿贾克斯部分和保存列表完美的作品

<div class="form-group"> 
             <label class="col-sm-2 control-label">Add New Custom Metrics</label> 
             <div class="col-lg-3"> 
              <input type="text" id="cus_metrics" name="cus_metrics" class="form-control" placeholder="" /> 
             </div> 
             <div class="col-lg-3"> 
           <div style="text-align: left;width:100%;height:100%;"> 
            <button type="button" id="addbtn" style="margin-left: 28%;width: 28%;" class="btn btn-primary">Add</button> 
           </div>            
             </div>           
           </div> 

根据我的Ajax代码,我送参数ID到JS文件,如查询所示,我试图使用where子句connection.query('select custom_metrics from metrics_list where custom_metrics = ?',[customMetric], function(err, result, response)

我想发送一个响应到HTML文件,并获得一个弹出的弹出警告框,如果该文件存在使用res.send方法,这显然不工作。请求你用完美的代码来帮助我,因为我一直坚持这一天!

帮助表示赞赏...

+4

'var res = JSON.stringify(result);'这就是为什么它不是一个函数 – vlaz

+0

ohhh woow thanks .. that worked!这是一个快速的答复... CHEERS哥们..我改变了我的代码,现在它完美的工作.. –

回答

0

我的服务器端JS与工作100%一个小的修改代码如下 -

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)+"ONLY RESULT :"+result+"****req.body.customMetric== "+customMetric); 
        //var res = JSON.stringify(result); 
        console.log("RESULTS LENGTH: "+result.length); 
        if(result.length > 0){       

         console.log("*****FOUND****"); 
         res.send('yes'); 
          return false; 

        } else { 

         console.log("NOT FOUND"); 

      // ******************************************** 
         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'); 
        } 
        }); 

       } 
      }); // select distinct close 

     //******************************************** 


        }// else result null 
       } // conn query check to validate 
      }); //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) {*/ 


     /*}*/ 
    } catch (ex) { 
     console.log("Exception : " + ex); 
    } 
}) 

和我的Ajax是 -

script type="text/javascript"> 
    $(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 <= 3 || customMetric.length >= 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 == 'yes') { 
          //alert("success"); 
          /* $("#success-alert").css("display", "block"); 
          top.location.href = "/dashboard";*/ 
          alert("GOT A yes"); 
          return false; 
         } else { 
          return true; 
         } 

       } 
      }); //$.ajax ends 
} 
     }); 
</script> 
相关问题