首页 > 解决方案 > 我应该如何处理来自 Mollie 授权 API 的分页 API 响应?

问题描述

我正在将 Mollie API 用于我正在创建的 SwiftUI 应用程序。我正在使用 firebase 云函数与 API 进行通信,并且需要列出当前用户的任务。我正在使用 Node.js Mollie 客户端与 API 进行通信。

以下行代码为我提供了包含相关信息的响应,但我无法访问响应的“_embedded”或“_links”参数中的实际数据。

const mandates = await mollieClient.customers_mandates.page({customerId:
    customerID});

我不断收到类似错误(如果我尝试访问“_embedded”中的“授权”):

Unhandled error TypeError: Cannot read property 'mandates' of undefined

我将响应保存在一个常量(命名为任务)中,并且能够使用console.log(mandates.count). 但是,这似乎不适用于“_embedded”和“_links”。

有关如何处理此问题的任何提示?提前致谢!

编辑:刚刚在节点客户端 GitHub 页面上找到以下代码片段作为示例。这与mollie 文档中提供的代码示例不同。尝试此代码会导致类似的错误,但发生得更早(甚至在给出响应之前):

Unhandled error TypeError: Cannot read property 'all' of undefined


/**
 * @docs https://docs.mollie.com/reference/v2/mandates-api/list-mandates
 */
const { createMollieClient } = require('@mollie/api-client');

const mollieClient = createMollieClient({ apiKey: 'test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM' });

(async () => {
  try {
    const mandates = await mollieClient.customerMandates.all({
      customerId: 'cst_pzhEvnttJ2',
    });

    console.log(mandates);
  } catch (error) {
    console.warn(error);
  }
})();

下面是 Mollie 文档提供的示例响应。

{
"count": 5,
"_embedded": {
    "mandates": [
        {
            "resource": "mandate",
            "id": "mdt_AcQl5fdL4h",
            "mode": "test",
            "status": "valid",
            "method": "directdebit",
            "details": {
                "consumerName": "John Doe",
                "consumerAccount": "NL55INGB0000000000",
                "consumerBic": "INGBNL2A"
            },
            "mandateReference": null,
            "signatureDate": "2018-05-07",
            "createdAt": "2018-05-07T10:49:08+00:00",
            "_links": {
                "self": {
                    "href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/mandates/mdt_AcQl5fdL4h",
                    "type": "application/hal+json"
                },
                "customer": {
                    "href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U",
                    "type": "application/hal+json"
                },
                "documentation": {
                    "href": "https://mollie.com/en/docs/reference/customers/create-mandate",
                    "type": "text/html"
                }
            }
        },
        { },
        { },
        { },
        { }
    ]
},
"_links": {
    "self": {
        "href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/mandates?limit=5",
        "type": "application/hal+json"
    },
    "previous": null,
    "next": {
        "href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/mandates?from=mdt_AcQl5fdL4h&limit=5",
        "type": "application/hal+json"
    },
    "documentation": {
        "href": "https://docs.mollie.com/reference/v2/mandates-api/revoke-mandate",
        "type": "text/html"
    }
}

}

标签: node.jsmollie

解决方案


推荐阅读