Error message I don't understand

Good morning
I am receiving this error message and I don’t understand what I should correct.
Do you have an idea ?

Navbars.js

import { BiShoppingBag, BiUser } from "react-icons/bi";
import { UserButton, useAuth } from "@clerk/nextjs";

export default function Navbar({ cart, onCartClick }) {
    const { isSignedIn } = useAuth();
    // Show Cart Quantity With Cart Icon
    const cartQuantity = cart.reduce((acc, item) => acc + item.quantity, 0)
    return (
        <nav className="bg-white p-3 md:p-4 mt-4 flex justify-between items-center rounded-lg">
            <a href="/" className="bg-gradient-to-tr from-orange-600 via-orange-700 to-orange-800 bg-clip-text text-transparent font-semibold text-2xl"
            >Store
            </a>
            <div className="flex items-center space-x-4">
                {/* Cart Icon */}
                <div className="relative">
                    <button onClick={onCartClick} className="text-black pr-2">
                        <BiShoppingBag size={24} className="inline" />
                        {cartQuantity > 0 && <span className="absolute top-[-10px] right-[-4px] bg-orange-700 text-white text-xs px-2 py-1 rounded-full">{cartQuantity}</span>}
                    </button>
                </div>
                {/* User Button */}
                {isSignedIn ? (
                    <UserButton afterSignOutUrl="/" />
                ) : (
                    <button onClick={() => (window.location.href = "/sign-in")} className="text-black">
                        <BiUser size={22} className="inline" />
                    </button>
                )}
            </div>
        </nav>
    );
}

create-checkout-session.js

import Stripe from "stripe";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);

export default async function handler(req, res) {
    if (req.method === "POST") {
        try {
            const { cart } = req.body;

            const baseUrl = process.env.NEXT_PUBLIC_BASE_URL

            const line_items = cart.map((item) => ({
                price_data: {
                    currency: "eur",
                    product_data: {
                        name: item.name,
                        images: [`${item.image}`],
                    },
                    unit_amount: item.price * 100,
                },
                quantity: item.quantity,
            }))

            const session = await stripe.checkout.sessions.create({
                payment_method_types: ["card"],
                line_items,
                mode: "payment",
                shipping_address_collection: "required",
                success_url: `${baseUrl}/success?session_id={CHECKOUT_SESSION_ID}`,
                cancel_url: `${baseUrl}/cancel`,
            })
            res.status(200).json({ id: session.id });
        } catch (error) {
            console.error("error creating checkout session", error);
            res.status(500).json({ error: error.message })
        }
    } else {
        res.status(405).json({ error: "Method not allowed" })
    }
}

Foreword: I have no idea what framework or environment you are using that generates these errors. I’ve never heard of something ‘hydrating’ in relation to webservers.

That said, if I had to purely guess from the error message, its unhappy because the object the Server got when it pulled a stripe session didnt match the object the Client got (because…yaknow… they’d be different objects if you called a session-based create command).

1 Like

It’s when a React (or Vue etc) app is server-rendered and sent as HTML to the client. Once that HTML arrives in the browser, React reconstructs the same app, attaching all the interactive parts to make it functional—essentially “hydrating” it. The benefit is that users see content faster while the JavaScript kicks in behind the scenes to make everything interactive.

1 Like

Thank you for your answer I followed this tutorial and I’m stuck at 1h33min and I don’t see how to solve it

https://www.youtube.com/watch?v=Xb27ZJ_F_iU

The first error suggests that there’s a mismatch between what was server-rendered and what’s generated in the client.

Common causes are dynamic values like Date.now() or Math.random() leading to different HTML on the server and client, conditional rendering that depends on client-only values (e.g. window), or HTML tag mismatches or incorrect nesting.

To address this make sure you don’t use such dynamic values values during server rendering. Inspect your React components for conditional parts that might behave differently during SSR and also for any client-specific logic, add checks like (typeof window !== 'undefined').


The second message indicates an invalid_request_error with "shipping_address_collection".

As far as I know, it should be:

shipping_address_collection: {
  allowed_countries: ['US', 'CA', 'FR'], // Add relevant countries here
},

This is untested, but should hopefully give you something to go on.

2 Likes

Thanks for your help it works with your code.

I still have an error message on the home page.
Do you have an idea ?

Yay!

Did you check all of the suggestions the error message made?

yes I read the error message but I don’t see what to do

I’m afraid I don’t have the time to comb through your code base for you to find the error. Maybe try leaving a comment on the YouTube video and see if anyone else has had the same problem? If it’s a genuine error in the code (as opposed to a typo on your part), then with a bit of luck, maybe the author will reply.

1 Like

Thank you for your help and patience