Next.js电商项目中Stripe listLineItems函数调用报错解决
解决Stripe listLineItems调用报错:TypeError: Cannot read properties of undefined (reading 'listLineItems')
这个报错的核心原因是Stripe API函数的调用路径拼写错误,你之前直接尝试调用的listLineItems并非顶层方法,因此会触发"读取undefined的属性"错误,同时也会因为找错层级导致在官方文档中找不到对应函数。
正确的调用路径是嵌套在checkout.sessions下的方法,正确写法示例:
// 初始化Stripe客户端 const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY); // 获取订单明细的正确写法 async function fetchOrderLineItems(sessionId) { try { const lineItems = await stripe.checkout.sessions.listLineItems(sessionId); return lineItems.data; } catch (error) { console.error('获取订单项失败:', error); throw error; } }
你之前找不到文档是因为找错了层级,这个方法属于Stripe Checkout Sessions的子方法,需要在Checkout相关的文档板块下查找。
内容的提问来源于stack exchange,提问作者nelakay
相关产品推荐
相关产品推荐

