'Getting a 400 Error while trying to decrease counter on clicking a button and saving that Data on Database

My Objective is to managing stock of Products. I have placed a button on product details page. On clicking that button, the stock of the products will be decreased by one and the updated stock will be stored in the Database.

What I've done shows the decrement of the products on the console, but 400 error shows along with it and the data is not getting saved on the Database.

The code I've written on the client-side-

        const newQty = parseInt(product.quantity) - 1;
        const quantity = newQty;
        console.log(quantity);
        const url = `http://localhost:5000/product/${productId}`;
        fetch(url, {
            method: 'PUT',
            headers: {
                'content-Type': 'application/json'
            },
            body: JSON.stringify(quantity)
        })
            .then(response => response.json())
            .then(data => {
                console.log(data);
                setProduct(data)
            })
    }

Code on the Server-Side-

            const id = req.params.id;
            const newQty = req.body;
            console.log(newQty);
            const filter = { _id: ObjectId(id) };
            const options = { upsert: true };
            const updatedQty = {
                $set: {
                    quantity: newQty.quantity
                }
            };
            const result = await inventoryCollection.updateOne(filter, updatedQty, options);
            res.send(result);
        })

The Error I am having- errorImage

any help on this matter will be highly appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source