我正在尝试检查mysql表中是否存在数据。我收到错误 TypeError:res.send不是函数。
我的mysql表有很多数据但有3列,所有varchar(45)和id(autoincremental)都是 -
**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页面中的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 == 'no') {
//alert("success");
/* $("#success-alert").css("display", "block");
top.location.href = "/dashboard";*/
alert("GOT A NO");
}
else{
alert("got a yes");
}
}
}); //$.ajax ends
}
});
</script>
我的HTML按钮指向js文件,我只添加按钮html,因为我只需要帮助ajax部分,保存和列表完美地工作
<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方法,则显示POP up警告框,这显然不起作用。请求您帮助我完成一些完美的代码,因为我现在已经坚持了一天!
HELP表示赞赏......
答案 0 :(得分: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>