Ich versuche zu überprüfen, ob Daten in MySQL-Tabelle vorhanden ist. Ich erhalte einen Fehler TypeError: res.send ist keine Funktion.Knoten js überprüfen, ob Daten in mysql Tabelle existiert.- TypeError: res.send ist keine Funktion
Meine MySQL-Tabelle hat eine Menge von Daten, sondern drei Spalten mit allen varchar (45) und id (autoincremental) die -
**id | metrics | custom_metrics**
1 | test | test
meine js Datei innerhalb Routen -
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);
}
})
Mein ajax-Aufruf in der hTML-Seite is-
<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 y HTML-Schaltfläche verweist auf die JS-Datei, ich bin auf den Button html nur das Hinzufügen da ich nur für den Ajax-Teil Hilfe benötigen und die speichern und Liste funktioniert perfekt
<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>
Nach meinem Ajax-Code, ich sende Parameter ID zu der JS-Datei und wie die Abfrage vorschlägt, versuche ich, eine where-Klausel connection.query('select custom_metrics from metrics_list where custom_metrics = ?',[customMetric], function(err, result, response)
zu verwenden.
Ich möchte eine Antwort auf die HTML-Datei senden und eine POP-up-Alarm-Box erhalten, wenn die Datei mit der res.send-Methode existiert, die anscheinend nicht funktioniert. Fordern Sie Sie auf, mir mit dem perfekten Code zu helfen, da ich jetzt für einen Tag mit dieser Sache festhalte !!
Hilfe ist willkommen ...
'var res = JSON.stringify (result);' deshalb ist es keine Funktion – vlaz
ohhh woow thanks .. das hat funktioniert !! Das war eine schnelle Antwort ... CHEERS Kumpel .. Ich habe eine Änderung in meinem Code gemacht, und jetzt funktioniert es perfekt .. –