【发布时间】:2020-09-14 06:24:41
【问题描述】:
我刚开始学习使用 Node、Express 和 MySQL 创建 rest API。我有我的函数 removeAll 不带参数,它将结果对象传递给箭头函数,然后运行一个 sql 函数,然后将另一个函数作为参数(err,res)。在嵌套箭头函数内部,如果我们没有任何错误并且受影响的行不等于 0,我们将得到返回 (null, res) 的结果。现在我的困惑是为什么结果包含 (null, res) 而不仅仅是 res。我也对实际如何分配 res 感到困惑,我看到它被传递到箭头函数中,但我们没有明确设置 res 是什么。
sql.query("DELETE FROM customers", (err, res) => {
if (err) {
console.log("error: ", err);
result(null, err);
return;
}
if (res.affectedRows == 0) {
// customer not found with the id
result({ kind: "not_found" }, null);
return;
}
console.log("deleted customer with id: ", id) {
result(null, res);
}
});
};
【问题讨论】: