我使用express.js查询时遇到问题。 Citta_p和Citta_a是两个数组,我想要的是我的查询给了我所有城市的所有ID。相反,我得到的只是最后一个值,它就像我的循环从var i = 1开始,我从来没有得到i = 0的值。任何人都可以帮助我吗? 这是我的代码:
page.html中
<div class = "container" id = "nuova_linea" style= "display : none">
<div class="row">
<div class="col-md-12" style = "margin-top: 100px;">
<form class="form-horizontal" method="post" action="/inseriscilinea">
<div class='alert alert-error'>
<button type='button' class='close' data-dismiss='alert'>×</button>
<fieldset>
<legend class="text-center header">Inserisci una nuova linea </legend>
<div class="form-group">
<label class="col-md-3 control-label" style = "margin-left: 200px">Nome Linea: </label>
<div class="col-md-5">
<input type="text" class="form-control" name="nome_linea" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" style = "margin-left: 200px">Numero fermate: </label>
<div class="col-md-1">
<input type="text" class="form-control" id="numero_fermate" name = "numero"></div>
<div><input type="button" onclick = "AggiungiFermate()" class = "btn btn-primary" id="button" value="Invia">
</div>
</div>
<div id = "contenitore"></div>
<div id = "miefermate" style = "display : none">
<div class="form-group">
<label class="col-md-3 control-label" style = "margin-left: 200px">Città di partenza: </label>
<div class="col-md-5">
<input type="text" class="form-control" name="nome_citta_p" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" style = "margin-left: 200px">Città di arrivo: </label>
<div class="col-md-5">
<input type="text" class="form-control" name="nome_citta_a" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" style = "margin-left: 200px">Km: </label>
<div class="col-md-5">
<input type="text" class="form-control" name="km" />
</div>
</div>
<div id = "salva" style = "display : none">
<div class="form-group">
<div class="col-md-5 col-md-offset-3">
<button type="submit" class="btn btn-primary">Salva</button>
</div>
</div>
</div>
</div>
</div>
</div>
<script type = "text/javascript">
function AggiungiFermate() {
for(var i=0;i<$("#numero_fermate").val();i++) {
$("#contenitore").append($("#miefermate").html())
}
$("#salva").show("slow");
}
</script>
page.js
exports.insertLine = function(req,res) {
var linea = req.body.nome_linea;
var numero = req.body.numero;
var citta_p = req.body.nome_citta_p;
var citta_a = req.body.nome_citta_a;
var km = req.body.km;
req.getConnection(function(err,connection) {
for(var i=0;i<numero;i++) {
var sql = connection.query("select c.id,cc.id from città as c, città as cc where (c.nome = ? and cc.nome = ?)",[citta_p[i],citta_a[i]], function(err,rows) {
if(err) console.log("%s", err);
if(rows.length > 0) {
console.log(rows);
}
}
});
}
});
};
答案 0 :(得分:0)
insertLine
应该是这样的:
// npm install async
var async = require('async');
exports.insertLine = function(req,res) {
var linea = req.body.nome_linea;
var numero = req.body.numero;
var citta_p = req.body.nome_citta_p;
var citta_a = req.body.nome_citta_a;
var km = req.body.km;
req.getConnection(function(err,connection) {
var q = "select c.id,cc.id from città as c, città as cc where (c.nome = ? and cc.nome = ?)";
async.times(numero,function(i,next){
connection.query(q,[citta_p[i],citta_a[i]],function(err,rows){
if(err) return next(err);
console.log(i,rows);
next(null,rows);
});
},function(err,rows){
if(err) res.send('error');
else if(!rows || !rows.length) res.redirect('/somewhere')
else res.send('ok');
});
});
};