【问题标题】:Stripe API Unhandled Rejection Response ErrorStripe API 未处理的拒绝响应错误
【发布时间】:2019-11-25 15:22:42
【问题描述】:

我只是使用 Stripe API 在 firebase 云函数中创建订阅。

我收到错误消息:未处理的拒绝 错误:发送后无法设置标头。

这是我的 index.ts:

export const subscribe = functions.https.onRequest((req, res) => {
    res.header('Content-Type','application/json');
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    //respond to CORS preflight requests
    if (req.method == 'OPTIONS') {
        res.status(204).send('');
    }

    stripe.subscriptions.create({
        customer: req.body.sid,
        items: [
          {
            plan: "plan_123123123",
          },
        ]
      }, function(err, subscription) {
          // asynchronously called
          if (err) {
            return res.send(err)
          }
          else {
              return res.send(subscription)
          }
        }
      );
});

【问题讨论】:

  • res.status(204).send(''); 之后尝试return;,否则,如果出现OPTIONS(飞行前)请求,您将调用res.send() 两次
  • 哇。我欠你一条命。一个 8 多小时的错误...非常简单,谢谢!
  • 在下面添加了答案。很高兴我有帮助:)

标签: node.js google-cloud-functions stripe-payments


【解决方案1】:

代替

if (req.method == 'OPTIONS') {
    res.status(204).send('');
}

尝试:

if (req.method == 'OPTIONS') {
    return res.status(204).send('');
}

否则,在 OPTIONS(飞行前)请求的情况下,您将调用 res.send() 两次,从而导致错误。

【讨论】:

    猜你喜欢
    • 2018-06-21
    • 2017-06-08
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 2019-10-27
    • 2017-04-04
    • 1970-01-01
    相关资源
    最近更新 更多