Error Image I am trying to create a shopping cart and once I get the data from the client side to the server side after each update, I am getting an internal server error even though everything is working fine and the cart data is being updated in the session.
here is the code from the client side:
const fetchData = async () => {
const res = await axios.get(`http://localhost:3000/cartdata`);
const data = res.data;
console.log(data);
calculateTotals(data);
remove(data);
// postData(data);
}
fetchData();
const postData = async (data) => {
// checkSlice(data);
axios.post('http://localhost:3000/cartdata',{
data: data
}).then((data) => {
console.log('posted', data)
}).catch((err) => {
console.log('error');
});
}
here is the code from the backend:
router.get('/cart', (req, res) => {
let sess = req.session;
let cart = (typeof sess.cart !== 'undefined') ? sess.cart : false;
console.log(cart)
res.render('tarpit/cart', {
pageTitle: 'Cart',
cart: cart,
nonce: Security.md5(req.sessionID + req.headers['user-agent'])
});
});
router.get('/cartdata', (req, res)=>{
let sess = req.session;
let cart = (typeof sess.cart !== 'undefined') ? sess.cart : false;
res.json(cart);
})
router.post('/cartdata',asyncError(async (req, res) =>{
req.session.cart = req.body.data
console.log(req.session.cart);
await req.session.cart.save();
}))
router.post('/cart',asyncError(async(req, res) => {
let qty = parseInt(req.body.qty, 10);
// console.log(qty);
let product = req.body.product_id;
// let format = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD'});
if(qty > 0 && Security.isValidNonce(req.body.nonce, req)) {
const mypro = await Products.findOne({_id: product})
let cart = (req.session.cart) ? req.session.cart : null;
const prod = {
id: mypro._id,
title: mypro.title,
price: mypro.price,
qty: qty,
image: mypro.image[0].url,
}
// res.send(prod)
Cart.addToCart(prod, qty, cart);
res.redirect('/cart');
}
else {
res.redirect('/');
}
}));
thanks