路线代码
router.post('/add', function(req, res) {
const compulsoryFields = ['primary_id', 'secondary_id', 'assessment_note_id', 'packing_list_id', 'model_id']
for(var i = 0; i < compulsoryFields.length; i++) {
if(req.body[compulsoryFields[i]] == '') {
req.flash('warning_msg', 'Please make sure all * fields are supplied')
res.redirect('/rmv/add')
return
}
}
async.series([
function(callback) {
Vehicle.existsByPrimaryId(req.body.primary_id, callback)
}, function(callback) {
Vehicle.existsBySecondaryId(req.body.secondary_id, callback)
}
], function(err, details) {
if(!details[0]) {
if(!details[1]) {
var vehicle = { id: null }
for(var key in req.body) {
vehicle[key] = req.body[key]
}
async.series([
function(callback) {
Vehicle.add(vehicle, callback)
}
], function(err, details_l2) {
if(details_l2[0]) {
req.flash('warning_msg', 'Vehicle added successfully')
res.redirect('/rmv/add')
} else {
req.flash('warning_msg', 'Error occured while adding vehicle. Please try again')
res.redirect('/rmv/add')
}
})
} else {
req.flash('warning_msg', 'Chassis/Frame number already exists')
res.redirect('/rmv/add')
}
} else {
req.flash('warning_msg', 'Engine/Motor number already exists')
res.redirect('/rmv/add')
}
})
})
型号代码
const MySql = require('../comms/rmv_mysql_con')
const Vehicle = module.exports = {}
Vehicle.add = function(vehicle, callback) {
MySql.pool.getConnection(function(pool_err, connection){
connection.query('INSERT INTO vehicle SET ?', vehicle, function(err, result) {
connection.release()
if(err) {
return callback(err, false)
}
callback(err, true)
})
})
}
Vehicle.getCount = function(callback) {
MySql.pool.getConnection(function(pool_err, connection) {
connection.query('SELECT COUNT(*) AS \'vehicle_count\' FROM vehicle', function(err, rows) {
connection.release()
if(err) {
return callback(err, false)
}
callback(err, rows[0]['vehicle_count'])
})
})
}
Vehicle.search = function(skw, callback) {
MySql.pool.getConnection(function(pool_err, connection){
connection.query('SELECT vehicle.*, primary_name, secondary_name, name FROM vehicle, model WHERE vehicle.model_id = model.id AND CONCAT(model_id, packing_list_id, assessment_note_id, primary_id, secondary_id, sale_date, customer_first_name, customer_last_name, customer_phone_number, customer_address_no, customer_address_street, customer_address_district, customer_nic, rmv_documents_received_date, rmv_documents_sent_date, cr_received_date, plate_number, plate_sent_date, cr_sent_date, remarks) LIKE \'%' + skw + '%\'', function(err, rows, fields) {
connection.release()
if(err) {
return callback(err, false)
}
callback(err, rows)
})
})
}
Vehicle.existsByPrimaryId = function(id, callback) {
MySql.pool.getConnection(function(pool_err, connection){
connection.query('SELECT * FROM vehicle WHERE primary_id = ?', id, function(err, rows, fields) {
connection.release()
if(rows.length > 0) {
return callback(err, true)
}
callback(err, false)
})
})
}
Vehicle.getAll = function(callback) {
MySql.pool.getConnection(function(pool_err, connection){
connection.query('SELECT vehicle.*, primary_name, secondary_name, name FROM vehicle, model WHERE vehicle.model_id = model.id', function(err, rows, fields) {
connection.release()
if(err) {
return callback(err, false)
}
callback(err, rows)
})
})
}
Vehicle.updateById = function(vehicle, id, callback) {
MySql.pool.getConnection(function(pool_err, connection){
connection.query('UPDATE vehicle SET ? WHERE id = ?', [vehicle, id], function(err, result) {
connection.release()
if(err) {
return callback(err, false)
}
callback(err, true)
})
})
}
Vehicle.getVehicleById = function(id, callback) {
MySql.pool.getConnection(function(pool_err, connection){
connection.query('SELECT vehicle.id, primary_id, sale_date, customer_first_name, customer_last_name, customer_phone_number, customer_address_no, customer_address_street, customer_address_district, customer_nic, rmv_documents_received_date, rmv_documents_sent_date, cr_received_date, plate_number, plate_sent_date, cr_sent_date, remarks, primary_name, secondary_name FROM vehicle, model WHERE vehicle.model_id = model.id AND vehicle.id = ?', id, function(err, rows, fields) {
connection.release()
if(err) {
return callback(err, false)
}
callback(err, rows)
})
})
}
Vehicle.existsBySecondaryId = function(id, callback) {
MySql.pool.getConnection(function(pool_err, connection){
connection.query('SELECT * FROM vehicle WHERE secondary_id = ?', id, function(err, rows, fields) {
connection.release()
if(rows.length > 0) {
return callback(err, true)
}
callback(err, false)
})
})
问题是,当添加到5-10个请求之间的某个地方后,服务器挂起来自快速调试的输出如下
/rmv/add
express:router router : /rmv/add +0ms
express:router dispatching POST /rmv/add +0ms
express:router trim prefix (/rmv) from url /rmv/add +0ms
express:router router /rmv : /rmv/add +0ms
express:router dispatching POST /add +0ms
express:router <anonymous> : /rmv/add +0ms
express:router <anonymous> : /rmv/add +1ms
express:router <anonymous> : /rmv/add +0ms
express:router dispatching GET /rmv/add +134ms
express:router query : /rmv/add +0ms
express:router expressInit : /rmv/add +0ms
express:router jsonParser : /rmv/add +0ms
express:router urlencodedParser : /rmv/add +0ms
express:router cookieParser : /rmv/add +0ms
express:router serveStatic : /rmv/add +0ms
express:router session : /rmv/add +4ms
express:router initialize : /rmv/add +0ms
express:router authenticate : /rmv/add +0ms
express:router <anonymous> : /rmv/add +4ms
express:router <anonymous> : /rmv/add +0ms
/rmv/add
express:router router : /rmv/add +1ms
express:router dispatching GET /rmv/add +0ms
express:router trim prefix (/rmv) from url /rmv/add +0ms
express:router router /rmv : /rmv/add +0ms
express:router dispatching GET /add +0ms
express:router <anonymous> : /rmv/add +0ms
express:router <anonymous> : /rmv/add +0ms
express:router <anonymous> : /rmv/add +0ms
当服务器挂起时,除了不使用asyncjs的请求之外,我发出的每个请求都被卡住了。服务器运行正常,重新启动后,在收到5-10个添加邮件路径的请求后再次出现问题。
答案 0 :(得分:1)
不知道它是否来自这里,但是在你的Vehicle模型的方法中,如果Mysql在你尝试连接池时返回错误,你也应该返回一个回调。
如果错误来自此处,可能会记录这些错误以进行测试。因为如果没有解决一个回调,那么你就无法访问async.series方法的最终函数。
async.series([
function(callback) {
Vehicle.existsByPrimaryId(req.body.primary_id, callback)
}, function(callback) {
Vehicle.existsBySecondaryId(req.body.secondary_id, callback)
}
], function() {
// can't access this if there is a pool error with
// vehicle.existsByPrimaryId or Vehicle.existsBySecondaryId,
// since callback is not resolved
});
之前:
Vehicle.updateById = function(vehicle, id, callback) {
MySql.pool.getConnection(function(pool_err, connection){
connection.query('UPDATE vehicle SET ? WHERE id = ?', [vehicle, id], function(err, result) {
connection.release()
if(err) {
return callback(err, false)
}
callback(err, true)
})
})
}
之后:
Vehicle.updateById = function(vehicle, id, callback) {
MySql.pool.getConnection(function(pool_err, connection){
if (poll_err) {
return callback(poll_err, false);
}
connection.query('UPDATE vehicle SET ? WHERE id = ?', [vehicle, id], function(err, result) {
connection.release()
if(err) {
return callback(err, false)
}
callback(err, true)
})
})
}