如何使用MongoDB的JS从对象中减少数量?
如何将项目数量从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论