Check the following code carefully for errors:
//修改接口
router.put("/update", async (req, res) => {
let { id, name } = req.body
console.log(id)
console.log(name)
const search_sql = `select * from category where id=?`
let { seach_err, search_rows } = await db.async.all(search_sql, [id])
console.log(seach_err)
console.log(search_rows)
if (search_rows == null) {
//要return 为了不让后面的继续res.send()
return res.send({
code: 498,
msg: "类别不存在,无法修改!"
})
}
//name的值需要用户自己填写 而id则是选中的一条category类别
const update_sql = `update category set name=? where id=?`
let { err, rows } = await db.async.run(update_sql, [name, id,])
if (err == null) {
res.send({
code: 200,
msg: "修改成功",
})
} else {
res.send({
code: 500,
msg: "修改失败"
})
}
})
Error correction:
will
let { seach_err, search_rows } = await db.async.all(search_sql, [id])
changed to
let { err: seach_err, rows: search_rows } = await db.async.all(search_sql, [id])