【发布时间】:2023-03-06 14:12:01
【问题描述】:
我在此处某处收到此警告,但我不知道为什么。我需要一些帮助来解决导致问题的原因。产品可以添加到数据库中,但我无法显示新添加的产品,直到我与用户注销并再次登录,然后用户的购物车显示已更新:
const Cart = props => {
const [products, addedProducts] = useState([])
useEffect(() => {
addedProducts(user.cart)
}, [])
const onRemove = () => toast.error('Product removed from cart!', {
position: "top-right",
autoClose: 2000,
hideProgressBar: true,
closeOnClick: true,
pauseOnHover: false,
draggable: true,
progress: undefined,
});
const handleRemove = (id) => {
fetch(`${API}/cart/${id}/delete`, {
method: 'POST'
}).then(() => {
onRemove();
const timer = setTimeout(() => {
props.history.push('/cart')
}, 500)
return () => clearTimeout(timer)
})
};
这是我的购物车控制器:
router.post('/usercart', async (req, res) => {
const [userId, cart] = req.body
console.log(req.body, 'ccccc')
try {
console.log(req.body, 'aft')
let userCart = await cartService.cart([userId, cart])
res.status(200).json({userCart})
} catch (error) {
res.status(401).json({ error: error })
}
})
这是我的购物车服务:
const cart = async ([userId, cart]) => {
let userCart = await User.updateOne({_id: userId}, {$push: {cart: cart}})
return userCart
}
【问题讨论】:
标签: node.js reactjs mongodb express mern