I have the following logic in my application: on the main page I have a list of products that I'm getting from my MongoDB database, each product has a quantity. When a user clicks on the button 'add to cart' next to the product, this product is saved in the local storage when the user adds more of a specific product, the available quantity is reduced (example: "product": "apple", "quantity": "10", when in cart: "quantity": 5 (user added 5 apples)).
I'm sending the list of added products as an order which is being saved on the backend in the database. My goal is to update the number of products on the main page, if user ordered 5 of 10 apples, there should be 5 apples left when I go to the home page. I'm not sure how to update it on the frontend. My Ember code looks like this:
cart.hbs:
<form onsubmit=>
controllers/cart.js:
createOrder(event){
event.preventDefault();
let order = this.store.createRecord('order', {
customer_name: this.name,
customer_phone: this.phonenumber,
products: this.get('cart.items'), // getting the array of products
});
order.save();
},
My node.js code looks like this:
routes/orders.js POST Requst
router.post('/', (req, res, next) => {
const order = new Order({
_id: new mongoose.Types.ObjectId(),
customer_name: req.body.order.customer_name,
total_price: req.body.order.total_price,
products: req.body.order.products,
});
order
.save()
.then(result => {
....
}
routes/products.js PATCH Request
const id = req.params.productId;
const updateOps = {};
for(const ops of req.body){
updateOps[ops.propName] = ops.value;
}
Product.update({ _id: id }, { $set: updateOps})
.exec()
.then(...)
All I need to do is get the available quantity of each product in the order and update it in my database with products with a PATCH request, the request should look like this:
"propName": "available_quantity",
"value": "" - the available quantity from the order
}]
I've been trying to figure it out for a couple of days and still no success. I think I just got lost in my own code logic.
Aucun commentaire:
Enregistrer un commentaire