**使用ID,我需要更新/编辑nodejs,expressjs中mysql数据库的现有名称,地址,注释**:
router.put("/update/:id", function(req, res) {
const { name, address, comment } = req.body;
if (!id || !name) {
return res
.status(400)
.send({ error: name, message: "Please provide name and id" });
}
dbConn.query(
"UPDATE clients SET name, address, comment = ? WHERE id = ?",
[name, address, comment, id],
function(error, results, fields) {
if (error) throw error;
return res.send({
error: false,
data: results,
message: "client has been updated successfully."
});
}
);
});
答案 0 :(得分:1)
应该执行以下操作:
const query = (query, params) => {
return new Promise((resolve, reject) => {
dbConn.query(
query,
params,
function(error, results, fields) {
if (error) return reject(error);
return resolve(results);
}
);
});
};
router.put("/update/:id", function(req, res) {
const { id } = req.params;
const { name, address, comment } = req.body;
if (!id || !name) {
return res
.status(400)
.send({ error: name, message: "Please provide name and id" });
}
query("UPDATE clients SET name, address, comment = ? WHERE id = ?", [name, address, comment, id])
.then(results => {
if (results && results.changedRows > 0) {
return res.send({
error: false,
data: results,
message: "client has been updated successfully."
});
}
else {
//handle no affected rows
}
})
.catch(err => {
//handle error
})
});