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

Node.js中Stripe创建订阅失败出现未处理Promise拒绝报错如何解决

问题原因分析
  • 直接诱因:你代码中随机拼接生成的customer ID cus_+随机串,不是你Stripe账户下真实存在的客户ID,调用Stripe创建订阅接口时会直接返回请求失败的错误。
  • 报错触发原因:async函数中没有添加错误捕获逻辑,接口调用抛出的异常没有被处理,导致Node抛出未处理Promise拒绝的警告。
修复方案
  1. 先通过Stripe后台或者Stripe客户创建接口,生成真实存在的customer ID,替换代码中随机生成的customer参数值
  2. 给async路由添加try/catch错误捕获逻辑,同时将错误信息返回便于后续排查
修正后代码示例
app.post('/createSubscription',async (req, res) => {
  try {
    // 注意这里替换为你Stripe账户下真实存在的customer ID
    const validCustomerId = 'cus_xxxxxxxxx'; 
    const subscription = await stripe.subscriptions.create({
      customer: validCustomerId,
      items: [
        {price: 'price_1InXJuIPT89VeZtCMeR3xQWf'},
      ],
    });
    // 正常返回订阅信息
    return res.status(200).json(subscription);
  } catch (err) {
    // 捕获Stripe接口调用错误,打印并返回错误信息
    console.error('创建订阅失败:', err);
    return res.status(400).json({
      error: err.message,
      details: err.raw
    });
  }
})

内容的提问来源于stack exchange,提问作者gianluca

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 13:06:01