【问题标题】:JSON/EJSON Error w/ Stripe API - Invoice Webhook Event带有 Stripe API 的 JSON/EJSON 错误 - 发票 Webhook 事件
【发布时间】:2019-05-18 12:05:49
【问题描述】:

更新与附加信息 - 12 月 18 日。

当我进一步测试时,似乎该错误与“this.response.end(json);”无关。请参阅以下注释:

条纹非发票事件: a) “this.response.end(EJSON.stringify(obj, {indent: true}));”工作并返回 200 b)“this.response.end(EJSON.stringify(obj));”本地服务器上没有错误,条带仪表​​板上出现“无法连接”错误并且不返回代码 200

条纹发票事件: a) “this.response.end(EJSON.stringify(obj, {indent: true}));”引发错误 - 请参阅下面的详细信息。 b)“this.response.end(EJSON.stringify(obj));”本地服务器上没有错误,条带仪表​​板上出现“无法连接”错误并且不返回代码 200

任何见解将不胜感激。

我在使用 Stripe API 时遇到了一些问题,正在寻求帮助或见解。我正在使用ngrok 在本地测试 Stripe webhook,一切正常,直到我向任何 Stripe 'invoice' 事件类型(例如 invoice.payment_succeeded)发出请求。当我对任何“发票”事件类型运行测试时,我会遇到几个错误:

  1. 我正在运行的应用程序中断(即要求我在终端中键入流星运行以重新启动应用程序)

  2. 我在终端中收到此服务器端错误消息:

///error message start///
events.js:183
     throw er; // Unhandled 'error' event
     ^

Error: write after end
   at write_ (_http_outgoing.js:622:15)
   at ServerResponse.write (_http_outgoing.js:617:10)
   at IncomingMessage.ondata (_stream_readable.js:639:20)
   at emitOne (events.js:116:13)
   at IncomingMessage.emit (events.js:211:7)
   at IncomingMessage.Readable.read (_stream_readable.js:475:10)
   at flow (_stream_readable.js:846:34)
   at resume_ (_stream_readable.js:828:3)
   at _combinedTickCallback (internal/process/next_tick.js:138:11)
   at process._tickCallback (internal/process/next_tick.js:180:9)
///error message end///
  1. 在我的 Stripe 仪表板上出现错误:'Test webhook error: Unable to connect'

同样,这仅在我向任何 Stripe 'invoice' 事件类型(例如 invoice.payment_succeeded)发出请求时才会中断。

我联系了 Stripe,想看看是否还有什么我应该考虑的,但他们说一切都很好。

最后一点,要让ngrok 运行,我使用'ngrok http 3000'

话虽如此,我的服务器端 webhook 代码如下。如果有人对导致此错误的原因有任何见解,我们将不胜感激。

///server side webhook code start///
Router.route('/webhooks/stripe', { where: 'server' })
   .get(function () {
       console.log('getter');
       this.response.end('closing...');
   })
   .post(function () {
       console.log('post function initiated');

       // stores payload as string
       var obj = this.request.body;

       console.log("print obj var");
       console.log(obj);

       // saves as indented string to send as response
       var json = EJSON.stringify(obj, {indent: true});

       this.response.writeHead(200, {
           'Content-Length': json.length,
           'Content-Type': 'application/json'
       });

       this.response.end(json);
   })
   .put(function () {
       console.log('put');
   });

///server side webhook code end///

【问题讨论】:

  • 只是好奇,直到哪一步才抛出错误? console.log(obj); 是否在抛出错误之前打印任何内容?
  • 嗨@wsw。它在“this.response.end(json);”处引发错误我能够通过一些控制台日志确认这一点。对我来说奇怪的是,代码在当天早些时候工作,然后突然对于发票事件类型,代码会中断并抛出错误 - 所有其他事件类型都可以工作。

标签: javascript node.js json meteor webhooks


【解决方案1】:

我能够解决这个问题。为了参考和后代,我删除了下面的注释代码并手动添加了我的状态代码(即 this.response.statusCode = 200;)。这使事情正常进行并解决了问题。

///code snippet start///
var json = EJSON.stringify(obj, {indent: true});

   //this is the code i removed
       // this.response.writeHead(200, {
       //     'Content-Length': json.length,
       //     'Content-Type': 'application/json'
       // });

   //this is the code i added to send a manual status code
       this.response.statusCode = 200;

       this.response.end(json);
///code snippet end///

【讨论】:

    猜你喜欢
    • 2021-12-17
    • 1970-01-01
    • 2020-01-21
    • 2014-12-17
    • 2015-10-24
    • 2019-04-14
    • 2021-11-28
    • 2022-10-18
    • 1970-01-01
    相关资源
    最近更新 更多