Hello there! I am currently attempting to send an array of objects to my MySql Database via an API endpoint.
Below is the code snippet from my API:
app.get("/orderdetails/add", (req, res) => {
const {
item__,
Qty_Ordered,
Unit_Price,
Ext_Price
} = req.query;
const INSERT_ORDERDETAILS_QUERY = `INSERT INTO oe_details (item__,
Qty_Ordered, Unit_Price, Ext_Price) VALUES('${item__}', '${Qty_Ordered}',
'${Unit_Price}', '${Ext_Price}')`;
connection1.query(INSERT_ORDERDETAILS_QUERY, (err, results) => {
if (err) {
return res.send(err);
} else {
return res.send("successfully added order details");
}
});
});
Furthermore, here is the function implemented in my Application:
addOrderDetails = _ => {
const o = this.props.o;
const summary = o.filter(function(obj) {
return obj.Quantity >= 1;
});
var url = "";
summary.forEach(function(e) {
url +=
"item__=" +
e.ExternalID +
"&Qty_Ordered=" +
e.Quantity +
"&Unit_Price=" +
e.Price +
"&Ext_Price=" +
e.ExtPrice +
"&";
});
url = url.trim("&");
fetch(`http://localhost:4000/orderdetails/add?${url}`).catch(err =>
console.error(err)
);
};
Upon running addOrderDetails, I noticed that the generated statement sent to MySql looks like this:
INSERT INTO oe_details (item__, Qty_Ordered, Unit_Price, Ext_Price)
VALUES('B-2080,B-2081', '8,5', '18.75,18.75', '150,93.75')
This situation seems incorrect... Is there a method I can use to insert multiple rows into the mysql database within the same context?
The process works correctly when adding only one row...
Your assistance would be greatly appreciated!
Thank you,