我的用例。
- 首先,我在表中插入一条记录
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实现这一点