Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am trying to pass cart products to a checkout but whenever i click on place order i'm redirected to the checkout page yes but the cart items do not appear there and the terminal says: Cart data in session: undefined
listCarts: []
here is my add to cart funtion in my server file:

Python
// Add-to-cart route
app.post('/add-to-cart', function (req, res) {
    const { productId, quantity } = req.body;
    console.log('Adding to cart - Product ID:', productId, 'Quantity:', quantity);

    const productDetails = getProductDetailsFromMenu(productId);
    console.log('Product Details:', productDetails);

    if (productDetails) {
        req.session.cart = req.session.cart || [];
        console.log('Existing Cart:', req.session.cart);

        const existingItem = req.session.cart.find(item => item.productId === productId);

        if (existingItem) {
            existingItem.quantity += quantity;
        } else {
            req.session.cart.push({
                productId: productId,
                name: productDetails.name,
                basePrice: productDetails.basePrice,
                price: productDetails.basePrice * quantity,
                quantity: quantity,
                image: productDetails.image,
            });
        }

        console.log('Updated Cart:', req.session.cart);

        // Redirect to the homepage or menu page
        res.redirect('/menu');
    } else {
        res.status(404).send('Product not found');
    }
});


What I have tried:

i have tried adding the following lines of codes to my checkout script:


HTML
<div class="col-md-12 col-lg-12">
                            <div class="odr-box">
                                <div class="title-left">
                                    <h3>Shopping cart</h3>
                                </div>
                                <div class="rounded p-2 bg-light">
                                    <% if (listCarts && listCarts.length > 0) { %>
                                        <% listCarts.forEach(item => { %>
                                            <div class="media mb-2 border-bottom">
                                                <div class="media-body">
                                                    <a href="detail.html"><%= item.name %></a>
                                                    <div class="small text-muted">
                                                        Price: $<%= item.basePrice.toLocaleString() %> 
                                                        | Qty: <%= item.quantity %> 
                                                        | Subtotal: $<%= item.price.toLocaleString() %>
                                                    </div>
                                                </div>
                                            </div>
                                        <% }); %>
                                    <% } else { %>
                                        <p>No items in the cart</p>
                                    <% } %>
                                </div>
                            </div>
                        </div>




// Retrieve cartData from localStorage
JavaScript
const cartData = localStorage.getItem('cartData');

        // Parse the cartData from JSON
        const listCarts = JSON.parse(decodeURIComponent(cartData));

        // Now, you can use the listCarts array to display the products on the checkout page
        console.log(listCarts);
Posted
Updated 9-Nov-23 22:21pm
v2
Comments
Chris Copeland 10-Nov-23 5:13am    
I assume this question is a follow-on from this question. Sufficed to say, in both questions you haven't actually provided the part of the code where you actually post to the '/add-to-cart' endpoint. And to be honest, in your existing question you're only storing the content of the cart in a "listCarts" variable.

How are you posting to '/add-to-cart'?
Andre Oosthuizen 10-Nov-23 12:13pm    
And the real issue really is because we are asking ChatGPT, copy and pasting the code as is without understanding what it should do - the following is straight out of GPT
Quote:const cartData = localStorage.getItem('cartData');

// Parse the cartData from JSON
const listCarts = JSON.parse(decodeURIComponent(cartData));

// Now, you can use the listCarts array to display the products on the checkout page
console.log(listCarts);

Thanks Chris for pointing out that 'in both questions you haven't actually provided the part of the code where you actually post to the '/add-to-cart' endpoint.', leaving us to guess what went south.

That leaves the OP with as olution without understanding a solution given by ChatGPT. It does help but you need to know when and where the given code errors and then fix the code to work for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900