【问题标题】:Stripe writable stream + nestjs [TypeError: Converting circular structure to JSON]Stripe 可写流 + nestjs [TypeError: Converting circular structure to JSON]
【发布时间】:2023-01-18 19:45:10
【问题描述】:

我将条纹发票和报价集成到我的 nestjs api 中。我正在尝试下载报价 PDF 选项,但节点 createWriteStream 出现问题。

Stripe API说实现代码如下

const {createWriteStream} = require("fs");

// Returns a stream.Readable
const pdf = await stripe.quotes
  .pdf("qt_0J1EnX589O8KAxCGEdmhZY3r");

await new Promise((resolve) => {
  pdf.pipe(createWriteStream("/tmp/tmp.pdf"));
  pdf.on("end", () => resolve());
})

我的代码

@Get('/:id/pdf')
async downloadQuote(@Param('id') id: string) {
    const pdf = await this._stripeClient.quotes.pdf(id)

    await new Promise<void>((resolve) => {
        pdf.pipe(createWriteStream('/tmp/tmp.pdf'))
        pdf.on('end', () => resolve())
    })


    return {
        success: true,
        data: pdf
    }
}

错误

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'IncomingMessage'
    |     property 'req' -> object with constructor 'ClientRequest'
    --- property 'res' closes the circle

【问题讨论】:

    标签: node.js express stripe-payments nestjs createwritestream


    【解决方案1】:

    您正在尝试将 pdf 转换为 Json。

    您的端点应该是这样的:

    @Get('/:id/pdf')
    async downloadQuote(@Param('id') id: string, @Res() res: Response) {
      const pdf = await this._stripeClient.quotes.pdf(id);
      pdf.pipe(res);
    }
    

    你可以参考这篇关于streaming files的NestJs指南。

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 2018-11-19
      • 2019-04-03
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 2017-08-13
      • 2022-12-26
      • 1970-01-01
      相关资源
      最近更新 更多