如何使用MongoDB的JS从对象中减少数量?

发布于 2025-01-26 04:33:09 字数 2187 浏览 1 评论 0原文

如何将项目数量从MongoDB中的对象减少,并在服务器站点中更新到客户端?

这是一个库存网站,我在这里存储这些物品的数量。单击“更新”按钮,数量应一一减少。 客户端站点代码:

const handleQuantity = (itemQuantity) => {
        console.log(itemQuantity);
        const count = (itemQuantity - 1);
        const update = { count };
        const url = `http://localhost:5000/item/${itemId}`
        console.log(url);
        fetch(url, {
            method: 'PUT',
            headers: {
                'content-type': 'application/json'
            },
            body: JSON.stringify(update)
        })
            .then(res => res.json())
            .then(data => setItemQuantity(parseInt(data.count)))
    }
    return (
        <div className='item mt-5 mx-auto pt-2 w-50'>
            <div><img width='200px' src={item.img} alt="" /></div>
            <div className='item-description'>
                <h3>{item.name}</h3>
                <p className='m-0'>ID: {item._id}</p>
                <p>{item.description}</p>
                <h5>Quantity: {item.quantity}</h5>
                <h3>Price: ${item.price}</h3>
                <h5>Supplier Name: {item.supplierName}</h5>
                <button onClick={() => handleQuantity(item.quantity)}>Delivered</button>
            </div>
        </div>
    );

服务器站点代码:

app.put('item/:id', async (req, res) => {
            const id = req.params.id;
            const quantity = req.body;
            console.log(quantity);
            const query = { _id: ObjectId(id) };
            const options = { upsert: true };
            const updateDoc = {
                $set: {
                    _id: id,
                    name: item.name,
                    price: item.price,
                    description: item.description,
                    quantity: quantity.quantity,
                    suppierName: item.suppierName,
                    img: item.img
                }
            }
            const result = await itemCollection.updateOne(query, updateDoc, options);
            res.send(result)

How can I reduce item quantity from an object in mongoDb and update in server site to client side?

Here is an inventory site where I stock the quantity of the items. clicking on the update button the quantity should reduce one by one.
client site code:

const handleQuantity = (itemQuantity) => {
        console.log(itemQuantity);
        const count = (itemQuantity - 1);
        const update = { count };
        const url = `http://localhost:5000/item/${itemId}`
        console.log(url);
        fetch(url, {
            method: 'PUT',
            headers: {
                'content-type': 'application/json'
            },
            body: JSON.stringify(update)
        })
            .then(res => res.json())
            .then(data => setItemQuantity(parseInt(data.count)))
    }
    return (
        <div className='item mt-5 mx-auto pt-2 w-50'>
            <div><img width='200px' src={item.img} alt="" /></div>
            <div className='item-description'>
                <h3>{item.name}</h3>
                <p className='m-0'>ID: {item._id}</p>
                <p>{item.description}</p>
                <h5>Quantity: {item.quantity}</h5>
                <h3>Price: ${item.price}</h3>
                <h5>Supplier Name: {item.supplierName}</h5>
                <button onClick={() => handleQuantity(item.quantity)}>Delivered</button>
            </div>
        </div>
    );

server site code:

app.put('item/:id', async (req, res) => {
            const id = req.params.id;
            const quantity = req.body;
            console.log(quantity);
            const query = { _id: ObjectId(id) };
            const options = { upsert: true };
            const updateDoc = {
                $set: {
                    _id: id,
                    name: item.name,
                    price: item.price,
                    description: item.description,
                    quantity: quantity.quantity,
                    suppierName: item.suppierName,
                    img: item.img
                }
            }
            const result = await itemCollection.updateOne(query, updateDoc, options);
            res.send(result)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文