【发布时间】:2017-12-24 02:23:49
【问题描述】:
我有一个 API,它返回一个留言板线程的回复列表(每次调用限制 5 个回复)。我想要做的是在响应中寻找特定的回复 uuid。如果未找到,请再次调用 AXIOS GET 以获取接下来的 5 个回复。
我想继续这个循环,直到调用 UUID 或 AXIOS GET 调用返回但没有结果。
示例 API 请求:
http://localhost:8080/api/v2/replies?type=thread&key=e96c7431-a001-4cf2-9998-4e177cde0ec3
示例 API 响应:
"status": "success",
"data": [
{
"uuid": "0a6bc471-b12e-45fc-bc4b-323914b99cfa",
"body": "This is a test 16.",
"created_at": "2017-07-16T23:44:21+00:00"
},
{
"uuid": "0a2d2061-0642-47eb-a0f2-ca6ce5e2ea03",
"body": "This is a test 15.",
"created_at": "2017-07-16T23:44:16+00:00"
},
{
"uuid": "32eaa855-18b1-487c-b1e7-52965d59196b",
"body": "This is a test 14.",
"created_at": "2017-07-16T23:44:12+00:00"
},
{
"uuid": "3476bc69-3078-4693-9681-08dcf46ca438",
"body": "This is a test 13.",
"created_at": "2017-07-16T23:43:26+00:00"
},
{
"uuid": "a3175007-4be0-47d3-87d0-ecead1b65e3a",
"body": "This is a test 12.",
"created_at": "2017-07-16T23:43:21+00:00"
}
],
"meta": {
"limit": 5,
"offset": 0,
"next_offset": 5,
"previous_offset": null,
"next_page": "http://localhost:8080/api/v2/replies?_limit=5&_offset=5",
"previous_page": null
}
循环将在 meta > next_page url 上调用 AXIOS GET,直到在结果中找到 uuid 或 meta > next_page 为空(意味着不再回复)。
【问题讨论】:
-
你试过什么?到目前为止,您似乎还没有做出任何努力。
-
一些上下文?这是在 Vue 方法中吗?
-
@DavidL 我已经玩了一段时间了。我尝试过传统的
while循环,由于 AXIOS 的承诺,它不起作用。我也尝试过构建一个 Promise while 循环 (gist.github.com/tangzhen/7b9434df8beac308fd3e),但这也行不通。 -
@BertEvans 它位于 Vue.js 2 组件的“已创建”挂钩内。
标签: loops while-loop promise vue.js axios