gpt4 book ai didi

javascript - 我怎样才能简化这个查询代码太长了?

转载 作者:行者123 更新时间:2023-11-29 19:26:25 25 4
gpt4 key购买 nike

谁能帮我简化这段代码..我需要使用存储过程来缩短或简化查询代码,但我仍然需要在“()”内指示详细信息。请帮助我在 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);
}
});

最佳答案

以下内容可能会有所帮助:

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
});
});
});
});

关于javascript - 我怎样才能简化这个查询代码太长了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42103596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com