【发布时间】:2022-11-17 01:13:31
【问题描述】:
我正在使用 Next.js 和 Stripe 建立一个电子商务商店。到目前为止一切正常。现在我正在订单页面中显示用户的订单,为此我使用了 Stripe 的 listLineItems 函数。
我遇到如下所示的错误: TypeError: Cannot read properties of undefined (reading 'listLineItems')
我不确定 listLineItems 函数是否仍然存在,因为我在他们的文档中找不到任何关于它的信息。
我包括了我为这个订单页面准备的代码作为上下文。有问题的功能在第二个 sn-p 中。
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();
这是使用 listLineItems 函数提取 Stripe 订单的代码。
//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,
},
};
}
不确定发生了什么,也许有人可以指出我正确的方向。
多亏了@pgs,我才发现函数中有一个拼写错误。
看来您可能缺少此处记录的功能上的 s,stripe.com/docs/api/checkout/sessions/line_items。它应该看起来像这样: await stripe.checkout.sessions.listLineItems(order.id, { limit: 100, }) 你能试试看这个问题是否仍然存在吗?
【问题讨论】:
-
看来您可能在此处记录的函数上缺少
s,stripe.com/docs/api/checkout/sessions/line_items。它应该看起来像这样: await stripe.checkout.sessions.listLineItems(order.id, { limit: 100, }) 你能试试看这个问题是否仍然存在吗? -
@pgs 感谢您的快速响应。这就是问题所在,它现在按预期工作了!
标签: reactjs firebase next.js stripe-payments next-auth