mysql - 将 MySQL 中最后插入的项目的 ID 发送到前端。(Express.js)
问题描述
我正在尝试将最后插入的元素的 id 发送到前端,但我不知道如何完成它。
我可以在后端成功检索元素 ID,但我不知道如何将其发送到前端。
async create(client){
const result = await connection.query(
`INSERT INTO ${table} (companyName, comments)\
VALUES (?, ?)`,
[client.companyName, client.comments], function(err, result, fields) {
if (err) throw err;
console.log("Last client inserted id = "+result.insertId)});
connection.commit();
return result;
}
这是路线的代码。我想将它附加到 res.json()
post((req , res) => {
//processOurCrapHere();
console.log(`Request Body= ${JSON.stringify(req.body)}/n`);
repository.create(req.body).then((result) =>{
console.log(req);
res.json({"result":"completed"});
});
});
解决方案
推荐阅读
- php - Wordpress 分页没有在页面上返回相同数量的帖子
- python - 检查数组以查找字典值中的匹配项并返回键
- objective-c - 苹果事件屏幕中的键盘 UI 问题
- apache-flink - Flink 丰富传入数据和处理时间
- python - 如何更改 pytest 类中参数化堆叠的顺序?
- php - 调用 SOAP 服务返回内部错误(来自服务器)
- python-3.x - Matplotlib 不保存图形所有带有轴的图形
- c - 当我尝试使用 C 在 ESP8266 上使用 DHT11 读取温度时得到“NaN”
- excel - 根据条件保存为 PDF
- angular - 多个表单控件的条件表达式在 @rxweb/reactive-form-validators@2.1.2 中不起作用