我在做一个关于自动时间表生成系统的项目。我是node.js的新手。这是一个将值表从html插入mysql的程序。当我运行这个程序时,它会导致死锁。
这是我的index.js文件。
var express=require('express');
var app=express();
app.set('view engine','ejs');
app.use(express.static('./public'));
app.use(express.static("."));
var Array=require('node-array');
var mysql = require('mysql');
var s=require('string');
//Connection pool
var pool=mysql.createPool({
connectionLimit:10,
host: "localhost",
user: "root",
password: "user@1235",
database:"time_table"
});
app.post('/expt2',urlencodedParser,function(req,res){
var table=req.body.table1;
console.log(req.body);
var data=[];
for(var key in (req.body)){
data.push(req.body[key]);
}
for(i=0;i<(data.length-1);i++)
console.log(data[i]);
var i=0;
while(i<(data.length-1))
{
lg=data[i];
console.log(lg.substr(2,1));
if(lg.substr(2,1)=="4")
{
sem=4 ;
room="lh1";
}
else if(lg.substr(2,1)=="6")
{
sem=6;
room="lh2";
}
else if(lg.substr(2,1)=="8")
{
sem=8;
room="lh3";
}
else
{
sem=5;
room="lh2";
}
day="Thrusday";
console.log(room);
console.log(sem);
pool.getConnection(function(err,con){
if (err) throw err;
console.log("Connected!");
console.log(lg);
pool.query("select fid from course where
cid='"+lg+"'",function(err,result)
{
if(err)throw err;
else
{
console.log("okay");
console.log(result);
Object.keys(result).forEach(function(key){
row=result[key];
console.log(row.fid);
ob=row.fid;
console.log(ob);
var sql="INSERT INTO classroom(sem,day,timings,fid,cid,room_name)
VALUES('"+sem+"','"+day+"','12:00:00','"+ob+"','"+cid+"','"+room+
"');"
con.query(sql,function(err,result)
{
if(err)throw err;
console.log("one row inserted");
//con.release();
});
});
}
});
});
i++;}
});
这是expt2.html文件。
<html>
<head>
<title>Master-Timetable</title>
<link rel="stylesheet"
href="/public/asset/css/bootstrap.min.css" type="text/css"/>
<script type="text/javascript" src="/public/asset/popper.min.js">
</script>
<script src="/public/asset/js/bootstrap.min.js"
type="text/javascript"></script>
<script src="/public/asset/jquery-3.2.1.min.js"
type="text/javascript"></script>
</head>
<body>
<form id="contacth" method="POST" action="/expt2"></form>
<table id="table1">
<tr class="head">
<th>Time->day|</th>
<th>7:30-8:30</th>
<th>8:30-9:30</th>
<th>9:30-10:30</th>
<th rowspan="19">BREAK</th>
<th>11:00-12:00</th>
<th>12:00-1:00</th>
<th rowspan="19">LUNCH BREAK</th>
<th>2:30-3:30</th>
<th>3:30-4:30</th>
<th>4:30-5:30</th>
</tr>
<tr>
<th rowspan="3" class="parent" value="monday">Monday</th>
<td class="child"><input type="text" maxlength="5" name="am1"
form="contacth"></td>
<td ><input type="text" maxlength="5" name="am2" form="contacth"/>
</td>
<td><input type="text" maxlength="5" name="am3" form="contacth"/>
</td>
<td><input type="text" maxlength="5" name="am4" form="contacth"/>
</td>
<td><input type="text" maxlength="5" name="am5" form="contacth"/>
</td>
<td><input type="text" maxlength="5" name="am6" form="contacth"/>
</td>
<td><input type="text" maxlength="5" name="am7" form="contacth"/>
</td>
<td><input type="text" maxlength="5" name="am8" form="contacth"/>
</td>
</tr>
<tr>
<td>
<input type="submit" name="t_save" value="save" class="btn btn-
success" form="contacth"/><br/>
</td>
</tr>
</table>
</body>
</html>
我得到了一个使用“async”和“await”的建议,但在我的代码中,我并没有真正了解到在哪里使用“async”和“await”。
1条答案
按热度按时间ctrmrzij1#
不要直接查询池。用con.query替换第一个pool.query。
看看这个问题:在node.js和node mysql模块中使用getconnection()和直接使用pool的区别?