Help guys im stuck. First of all im a beginner. This is my first react project
I have a form being sent with f_name, l_name, order order is an array of orders I am trying to loop through it and find the corresponding product then subtract the quantity for that order in the available stocks.
let Transaction = require("../models/transactionModel");
let Products = require("../models/userInventoryModel");
router.route("/add").post(async (req, res) => {
const { f_name, l_name, order } = req.body;
try {
const newTransaction = new Transaction({
f_name,
l_name,
order,
});
await order.forEach((order) => {
let newProduct = Products.findOneAndUpdate(
{ product: order.product },
{ $inc: { stocks: -order.quantity } }
);
newProduct.save();
});
await newTransaction.save();
res.status(200).json(newTransaction);
} catch (error) {
res.status(400).json(error.message);
}
});
question from:https://stackoverflow.com/questions/65907436/subtract-value-in-a-document-in-api-request-mongodb