Home > Enterprise >  Stripe listLineItems function not working
Stripe listLineItems function not working

Time:11-17

I am setting up an e-commerce store with Next.js and Stripe. Everything is working well so far. Now I am in the process of displaying the user's orders in an orders page, for which I am using Stripe's listLineItems function.

I am encountering an error that looks as follows: TypeError: Cannot read properties of undefined (reading 'listLineItems')

I am unsure as to whether the listLineItems function still exists, as I cannot find anything in their documentation about it.

I am including the code I have for this orders page for context. The function in question is in the second snippet.

import { getSession, useSession } from "next-auth/react";
import React from "react";
import Header from "../components/Header";
import moment from "moment";
import db from "../../firebase";
import Order from "../components/Order";

function Orders({ orders }) {
  const { data: session } = useSession();

  return (
    <div className="bg-celeste_color">
      <Header />
      <main className="max-w-screen-lg mx-auto p-10">
        <h1 className="text-3xl border-b mb-2 pb-1 border-celeste_color-purple text-celeste_color-gray">
          Your Orders
        </h1>

        {session ? (
          <h2>x Orders</h2>
        ) : (
          <h2>Please sign in to see your orders</h2>
        )}
        <div className="mt-5 space-y-4">
          {/* Optional chain. If undefined, do not freak out.  */}
          {orders?.map(({ id, amount, items, timestamp, images }) => (
            <Order 
            key={id}
            id={id}
            amount={amount}
            items={items}
            timestamp={timestamp}
            images={images}
            />
          ))}
        </div>
      </main>
    </div>
  );
}

export default Orders;

export async function getServerSideProps(context) {
  const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);

  //Get the users logged in credentials
  const session = await getSession(context);

  if (!session) {
    return {
      props: {},
    };
  }

  //Firebase db
  const stripeOrders = await db
    .collection("users")
    .doc(session.user.email)
    .collection("orders")
    .orderBy("timestamp", "desc")
    .get();

Here is the code that is pulling in the Stripe orders with the listLineItems function.

  //Stripe orders
  const orders = await Promise.all(
    stripeOrders.docs.map(async (order) => ({
      id: order.id,
      amount: order.data().amount,
      images: order.data().images,
      timestamp: moment(order.data().timestamp.toDate()).unix(),
      items:
        //asynchronous call to call in the information we are going to access with .data
        (
          await stripe.checkout.session.listLineItems(order.id, {
            limit: 100,
          })
        ).data,
    }))
  );

  return {
    props: {
      orders,
    },
  };
}

Not sure what's going on, maybe someone can point me in the right direction.

Thanks to @pgs I was able to figure out that there is a typo in the function.

It appears that you might be missing an s on the function as documented here, stripe.com/docs/api/checkout/sessions/line_items. It should instead look like this: await stripe.checkout.sessions.listLineItems(order.id, { limit: 100, }) Could you try this and see if the issue persists?

CodePudding user response:

Great to hear hat it's working now, moving the response here:

It appears that you might be missing an s on the function as documented here. It should instead look like this:

await stripe.checkout.sessions.listLineItems(order.id, { 
  limit: 100, 
}) 
  • Related