mysql-js如何插入具有插入ID的批量记录

时间:2018-07-22 06:12:00

标签: mysql node.js mysqljs

我的用例。

  1. 首先,我将一个单个记录插入grn_master表中。
  2. 然后我得到insertedId,将多个记录插入到grn_detail表中。

这是我的代码。

async function create(req, res, next) {
    const grn = req.body.grn;
    const supplier = req.body.supplier_id; //An object contains objects
    const location = req.body.location;
    try {
        let query = "INSERT INTO grn_master SET customer_supplier_id = ?,location_id =?";
        connection.query(query, [supplier, location], (error, results) => {
            if (error) {
                console.log(error);
                res.sendStatus(500);
            } else {
                const grn_number = results.insertedId;
                let query = "INSERT INTO grn_detail SET item_id=?,unit_price=?,qty=?,total=?,grn_master_id=??";

                connection.query(query, [grn, grn_number], (error, results) => {
                    if (error) {
                        res.sendStatus(500);
                        console.log(error);
                    } else {
                        res.sendStatus(200);

                    }
                })


            }
        })
    } catch (error) {
        console.log(error);
        res.sendStatus(500);
    }
}

如何使用Mysql-js

任何帮助表示感谢。

0 个答案:

没有答案