在生产中出现此错误,但在开发中工作正常 - StripeInvalidRequestError: The `line_items` parameter is required in payment m
Posted
技术标签:
【中文标题】在生产中出现此错误,但在开发中工作正常 - StripeInvalidRequestError: The `line_items` parameter is required in payment mode【英文标题】:Getting this error in production but works fine in development - StripeInvalidRequestError: The `line_items` parameter is required in payment mode 【发布时间】:2021-07-12 13:29:54 【问题描述】:我有一个我最近部署的节点应用程序,并且在生产中从条带/结帐/会话 api 收到错误,但从未在开发中。错误:“StripeInvalidRequestError: The line_items
parameter is required in payment mode”非常简单,所以我知道我的 line_items 对象是空的,但我不知道为什么,因为这只发生在生产中,但在开发中工作正常。
这是该路线的代码
router.get('/checkout', async (req, res) =>
if (!req.session.cart)
return res.redirect('shop/shopping-cart');
try
let cart = new Cart(req.session.cart);
const cartProducts = await cart.generateArray();
let lineItems = [];
cartProducts.forEach(async (cartProduct) =>
let productImages = new Array();
cartProduct.item.images.forEach((img) =>
productImages.push(img.url);
);
const product = await stripe.products.create(
name: cartProduct.item.name,
images: productImages
);
const price = await stripe.prices.create(
product: product.id,
unit_amount: cartProduct.price,
currency: 'usd'
);
lineItems.push( price: price.id, quantity: cartProduct.qty );
);
let custObj = ;
if (process.env.NODE_ENV !== 'production')
custObj =
payment_method: 'pm_card_visa',
invoice_settings:
default_payment_method: 'pm_card_visa',
const customer = await stripe.customers.create(custObj);
const success_url = process.env.NODE_ENV !== 'production' ?
'http://localhost:3000/shop/success?session_id=CHECKOUT_SESSION_ID' :
'https://aboutthatwedding.com/shop/success?session_id=CHECKOUT_SESSION_ID';
const cancel_url = process.env.NODE_ENV !== 'production' ?
'http://localhost:3000/shop/shopping-cart' :
'https://aboutthatwedding.com/shop/shopping-cart';
const session = await stripe.checkout.sessions.create(
customer: customer.id,
payment_method_types: ['card'],
line_items: lineItems,
success_url: success_url,
cancel_url: cancel_url,
mode: "payment",
billing_address_collection: 'required',
shipping_address_collection:
allowed_countries: ['US', 'CA'],
);
res.render('shop/checkout',
sessionId: session.id,
products: cartProducts,
totalPrice: cart.totalPrice,
stripePubKey: process.env.NODE_ENV !== 'production' ?
process.env.STRIPE_PUBLIC_KEY_TEST :
process.env.STRIPE_PUBLIC_KEY_LIVE
);
catch (error)
console.log(error);
);
【问题讨论】:
【参考方案1】:看起来这是一个 foreach(async...) 问题。我将函数更改为将 cartProducts 和图像迭代为 for of 循环而不是 forEach 循环。
从This similar question that I found after posting my question获得该信息
【讨论】:
是的,您需要在创建会话之前await
生成您的订单项,否则这将发生异步并且会话将在没有填充项目的情况下继续进行。很高兴你把它解决了!以上是关于在生产中出现此错误,但在开发中工作正常 - StripeInvalidRequestError: The `line_items` parameter is required in payment m的主要内容,如果未能解决你的问题,请参考以下文章
NuxtJS:在开发中工作但在生产中工作的路由(netlify)
Taiwlind CSS:样式在生产中不加载,在开发中工作。将 Next.js 与 MDX 一起使用