1

我正在使用 loopback-next 和 stripe api。在条带 API 中,我在 payment.controller.ts 文件中按如下方式调用检索帐户:

@post('/payments/retrieve-stripe/{id}', {
    responses: {
      '200': {
        description: 'User model instance',
        content: {'application/json': {schema: {'x-ts-type': User}}},
      },
    },
  })
  async retrieveStripe(@param.path.number('id') id: number,
  @requestBody() req: any): Promise<any> {
    console.log(req);
    if (!req.stripeAccountId) {
      throw new HttpErrors.NotFound('No Stripe Account');
    }
    else {
    return await stripe.accounts.retrieve(
  req.stripeAccountId,
  function(err: any, account: any) {
    return err ?  err : account
  })
    }
  }

但是,当我尝试返回帐户时,JSON 正文中没有返回任何内容。如果我尝试,前端的 response.json,它说 JSON 意外完成,这意味着正文中没有任何内容。我如何能够在控制器函数内的上述函数中成功返回帐户?

这与我尝试返回字符串时遇到的问题相同。我不知道该怎么办。

编辑:我了解到您不能在回调中返回变量,这就是问题所在。

4

1 回答 1

1

您必须要求类型定义(@types/stripe)才能以承诺风格使用其库。之后,您可以通过以下方式使用:-

 @post('/payments/retrieve-stripe/{id}', {
    responses: {
      '200': {
        description: 'User model instance',
        content: { 'application/json': { schema: { type: 'object' } } },
      },
    },
  })
  async retrieveStripe(@param.path.number('id') id: number,
    @requestBody() req: any): Promise<any> {
    console.log(req);
    if (!req.stripeAccountId) {
      throw new HttpErrors.NotFound('No Stripe Account');
    } else {
      return await stripe.accounts.retrieve(req.stripeAccountId).then((res: any) => {
        return res;
      }).catch((err: any) => {
        console.debug(err);
        throw new HttpErrors.InternalServerError('Something went wrong!')
      });
    }
  }

有关更多详细信息https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/stripe/stripe-tests.ts

于 2019-09-12T03:58:44.163 回答