首页 > 解决方案 > 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] ]

标签: javascriptnode.jsjsonstripe-payments

解决方案


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.


推荐阅读