0

Can someone explain to me why 'map' is undefined? I provided my server.js and checkout.js below

The problem is that when I click a product to add to the cart to checkout it shows all the products, including the prices and total, on the stripe session instead of the item I selected. My goal is to only show one of the items selected from the actual website

server.js

//require("dotenv").config()

// This is your test secret API key.
const stripe = require("stripe")(
  "sk_test_XXXXXX"
);

const express = require("express");
const app = express();
app.use(express.static("public"));
app.use(express.json());

const YOUR_DOMAIN = "http://localhost:4242";

const storeItems = new Map([
  [1, { priceId: "price_XXXXX", name: "Black Sweatsuit" }],
  [2, { priceId: "price_XXXXX", name: "Grey Sweatsuit" }],
  [3, { priceId: "price_XXXXX", name: "Red Sweatsuit" }],
  [4, { priceId: "price_XXXXX", name: "Blue Sweatsuit" }],
  [5, { priceId: "price_XXXXX", name: "Black T-Shirt" }],
  [6, { priceId: "price_XXXXX", name: "Blue T-Shirt" }],
  [7, { priceId: "price_XXXXX", name: "Purple T-Shirt" }],
]);

app.post("/create-checkout-session", async (req, res) => {
  const session = await stripe.checkout.sessions.create({
    line_item: req.body.items.map((item) => {
      const storeItem = storeItems.get(item.id);
      return {
        price_data: {
          currency: "usd",
          product_data: {
            name: storeItem.name,
          },
          unit_amount: storeItem.priceId,
        },
      };
    }),
    mode: "payment",
    success_url: `${YOUR_DOMAIN}/success.html`,
    cancel_url: `${YOUR_DOMAIN}/cancel.html`,
    automatic_tax: { enabled: true },
  });

  res.redirect(303, session.url);
});

app.listen(4242, () => console.log("Running on port 4242"));

checkout.js

const button = document.getElementById("checkout-button");
button.addEventListener("click", () => {
  fetch("/create-checkout-session", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      items: [
        // { id: 1, quantity: 1 },
        // { id: 2, quantity: 1 },
        // { id: 3, quantity: 1 },
        // { id: 4, quantity: 1 },
        // { id: 5, quantity: 1 },
        // { id: 6, quantity: 1 },
        // { id: 7, quantity: 1 },
      ],
    }),
  })
    .then((res) => {
      if (res.ok) return res.json();
      return res.json().then((json) => Promise.reject(json));
    })
    .then(({ url }) => {
      window.location = url;
    })
    .catch((e) => {
      console.error(e.error);
    });
});
  • 2
    `map` isn't undefined, `req.body.items` is undefined – Nick Nov 09 '22 at 02:54
  • What does the HTML look like for the `#checkout-button` element? Is it a submit button within a `
    ` tag by any chance?
    – Phil Nov 09 '22 at 02:54
  • FYI don't respond with a redirect to an async call. It never works how you think it will. See [What is the difference between post api call and form submission with post method?](https://stackoverflow.com/a/58230870/283366) – Phil Nov 09 '22 at 02:57
  • @Nick ok I see how would I call the function and get the items to correspond individually? –  Nov 09 '22 at 03:21
  • @Phil yes its a submit button within a form –  Nov 09 '22 at 03:22
  • You might need to use something like the body-parser middleware: https://github.com/expressjs/body-parser. – Nick Nov 09 '22 at 03:23
  • In that case, your form is submitting normally and any async requests will be aborted. Change the button type to `type="button"` or prevent the default event action from triggering – Phil Nov 09 '22 at 03:26
  • Does this answer your question? [Stop form refreshing page on submit](https://stackoverflow.com/questions/19454310/stop-form-refreshing-page-on-submit) – Phil Nov 09 '22 at 03:27
  • @Phil No it doesn't, my goal is to figure out how can i define req.body.items –  Nov 10 '22 at 16:38
  • I think you missed the point. If the form submits regularly, your `fetch()` request won't even happen. Did you try changing the button type? If so and you're still having issues, please [edit] your question to include the HTML – Phil Nov 10 '22 at 20:34

0 Answers0