任何人都可以帮我简化这段代码.. 我需要使用存储过程缩短或简化查询代码,但仍然需要在"()"内部进行指示。细节.. 请在node.js
中帮助我router.post('/test', function (req, res, next) {
try {
var reqObj = req.body;
console.log(reqObj);
req.getConnection(function (err, conn) {
if (err) {
console.error('SQL Connection error: ', err);
return next(err);
} else {
var employee_name = reqObj.employee_name;
var employee_contact = reqObj.employee_contact;
var employee_position = reqObj.employee_position;
我想让这个查询简短,因为我计划输入大量数据,以便如何简化这个?
var query = conn.query(
"CAll test3("+"'"+employee_name+"','"+employee_contact+"','"+employee_position+"')",
function (err, result) {
if (err) { console.log(query)
console.error('SQL error: ', err);
return next(err);
}
console.log(result);
var test_Id = result.insertId;
res.json({
"test_id": test_Id
});
});
}
});
} catch (ex) {
console.error("Internal error:" + ex);
return next(ex);
}
});
答案 0 :(得分:0)
以下内容可能有所帮助:
router.post('/test', (req, res, next) => {
const makeQuery = data => {
var { employee_name, employee_contact, employee_position } = data;
var query = 'CAll test3(?, ?, ?)';
var data = [employee_name, employee_contact, employee_position];
return [query, data];
};
req.getConnection((err, conn) => {
if (err) return next(err);
conn.query(...makeQuery(req.body), (err, res) => {
if (err) return next(err);
res.json({
"test_id": res.insertId
});
});
});
});