我的用例。
grn_master
表中。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
任何帮助表示感谢。