javascript - Stripe api 中的 stringify 超出了最大调用堆栈大小
问题描述
我在我的后端节点 js 中使用条带 api。
const checkOutSession = asyncHandler(async(req,res)=>{
const checkOut = await CheckOut.findById(req.params.id)
console.log(checkOut.lineItems)
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items : checkOut.lineItems ,
mode: 'payment',
success_url: `${process.env.BASE_URL}/payment?success=true`,
cancel_url: `${process.env.BASE_URL}/payment?canceled=true`,
})
res.json({sessionId : session.id})
})
我的模型是这样的
const checkOutSchema = new mongoose.Schema({
user:{
type: mongoose.Schema.Types.ObjectId,
ref:'user',
required:true
},
lineItems:[{
_id:false,
price_data:{
currency: {
type:String,
default: 'usd'
},
product_data:{
name:{
type:String,
required:true
},
images:[String]
},
unit_amount:{
type:Number,
required:true
},
},
quantity:{
type:Number,
required:true
}
}],
isPaid:{
type:Boolean,
default:false
}
})
我认为问题在于 line_items:checkOut.lineItems。当我对 line_items 进行硬编码时,它可以正常工作,但是当我在 line_items 中使用 checkOut.lineItems 时,出现如下错误:
RangeError:最大调用堆栈大小超过 [0] 在 Function.[Symbol.hasInstance] () [0] 在 stringify (F:\Coding Projects\Mern\Mobile\node_modules\qs\lib\stringify.js:65:20) [0] 在 stringify (F:\Coding Projects\Mern\Mobile\node_modules\qs\lib\stringify.js:125:33)
当我 console.log checkOut.lineItems 时,它以条带文档中显示的格式正确记录。console.log 显示为
[
[0] {
[0] price_data: { product_data: [Object], currency: 'usd', unit_amount: 499 },
[0] quantity: 1
[0] },
[0] {
[0] price_data: { product_data: [Object], currency: 'usd', unit_amount: 499 },
[0] quantity: 1
[0] }
[0] ]
它仅在我对该值进行硬编码而不是动态编码时才有效。如果我 console.log JSON.Stringify(checkOut.lineItems) 它就像
[
[0] {
[0] "price_data": {
[0] "product_data": {
[0] "images": [
[0] "https://res.cloudinary.com/saralkarki/image/upload/v1620276206/MobiHub/MI/Mi%2011%20Lite/xiaomi-mi-11-lite-4g-blue-600x600_v8r10l.jpg"
[0] ],
[0] "name": "Xiaomi Mi 11 Lite"
[0] },
[0] "currency": "usd",
[0] "unit_amount": 499
[0] },
[0] "quantity": 1
[0] },
[0] {
[0] "price_data": {
[0] "product_data": {
[0] "images": [
[0] "https://res.cloudinary.com/saralkarki/image/upload/v1620275521/MobiHub/MI/Redmi%20Note%2010/xiaomi-redmi-note-10-thumb-green-600x600_ntavlj.jpg"
[0] ],
[0] "name": "Xiaomi Redmi Note 10"
[0] },
[0] "currency": "usd",
[0] "unit_amount": 499
[0] },
[0] "quantity": 1
[0] }
[0] ]
解决方案
This seems unrelated to the stripe-node package, and only an issue with stringifying the object. Possibly your checkOut.lineItems
is some non-standard structure from the mongo scheme. You say it logs correctly with JSON.stringify
but the error shows that qs.stringify
is being used, possible related to the mongo toString()
definition.
To test this, you could try:
line_items: JSON.parse(JSON.stringify(checkOut.lineItems)),
if that works, you're at least right that all the needed data is there and you have a problem with stringification to resolve. I wouldn't recommend the above outside of a debugging context.
推荐阅读
- c# - C# ListBox如何从listbox中导出我传入的名字的文件
- javascript - 在源代码中使用 `index.d.ts` 声明文件
- java - Java:在以下情况下如何从保存文件中加载?
- python - 如何在 python 中正确运行 .exe 文件?
- z3 - 为什么 z3 horn 求解器结果未知,为什么这是未知原因
- sql - 如何在sql中编写此连接条件?
- react-native - react-native - 模态总是可见的
- ios - Swift KVO 持有对象引用计数
- c# - Xamarin Forms 在应用程序进入后台时运行代码(即进入睡眠)
- git - 为什么 Tortoise GIT 在提交期间将状态显示为版本化文件的重命名