首页 > 解决方案 > 将 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"});
      });
    });

标签: mysqlangularexpress

解决方案


推荐阅读