You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Stripe支付日志打印失败:循环结构转JSON错误排查求助

问题根源

「Converting circular structure to JSON」错误是因为API网关控制器中传递了带有循环引用的res对象到消息队列。NestJS的Response对象内部存在循环引用(比如res引用req,req又反向引用res),当框架尝试将消息 payload 序列化为JSON时就会触发这个错误。

解决步骤

1. 修正API网关控制器,传递正确的会话参数

原代码错误地将res作为消息内容发送,实际应该从请求中提取session_id参数,传递给消息队列:

@Get('pay/success/checkout/session')
async paymentSuccess(@Query('session_id') sessionId: string) {
  return this.authServico.send(
    { cmd: 'payment-succes' },
    { sessionId } // 传递Stripe会话ID而非Response对象
  );
}

2. 在消息队列服务中获取完整的Stripe会话信息

接收到session_id后,调用Stripe API获取完整的会话详情,再传递给业务逻辑:

// 确保已导入并初始化Stripe客户端
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);

@MessagePattern({ cmd: 'payment-succes'})
async pagamentoPS(@Ctx() contexto: RmqContext, @Payload() payload: { sessionId: string }){
  const channel = contexto.getChannelRef()
  const message = contexto.getMessage()
  channel.ack(message)
  
  // 调用Stripe API拉取会话详情
  const session = await stripe.checkout.sessions.retrieve(payload.sessionId);
  return this.reservaService.SuccessSession(session)
}

3. 安全打印Stripe会话对象(可选)

Stripe返回的会话对象可能包含内部循环引用,直接console.log可能仍有问题,可使用Node.js的util.inspect工具打印:

import { inspect } from 'util';

async SuccessSession(Session: Stripe.Checkout.Session) {
  // 打印完整会话信息,支持循环引用
  console.log(inspect(Session, { depth: null, colors: true }));
  
  // 或者只打印关键字段,避免冗余
  console.log(`会话ID: ${Session.id}, 支付状态: ${Session.payment_status}`);
}

内容的提问来源于stack exchange,提问作者J. Maciel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 19:18:41