目前我需要在下一个axios get中使用axios响应。
首先得到:
第一个获取返回的版本ID,例如 10534 。
TTFB of https://www.example.com/ is: 0.67417
第二次获得:
现在我需要在下一个请求中包含 versionID
axios.get('https://jira.example.co.uk/rest/api/2/project/PAD/versions', headers)
.then(function(response) {
const versionID = response.data.map(function(version){
const vId = version.id;
return vId;
});
return versionID;
})
.catch(err => {
console.error(err, err.stack);
});
我将如何实现这一目标?
答案 0 :(得分:1)
axios.get('https://jira.example.co.uk/rest/api/2/project/PAD/versions', headers)
.then(function(response) {
const versionID = response.data.map(function(version){
const vId = version.id;
return vId;
});
getAnotherRequest(versionID);
})
.catch(err => {
console.error(err, err.stack);
});
getAnotherRequest(versionID){
axios.all([
axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers),
axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=status IN (Reported) AND project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers),
axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=status IN (Confirmed) AND project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers)
])
.then(axios.spread(function (response1, response2, response3) { ... }
}
但请检查您的versionID
数组而不是整数,因为它是map
的结果,而map
的结果是数组。
答案 1 :(得分:0)
axios.get('https://jira.example.co.uk/rest/api/2/project/PAD/versions', headers)
.then(function(response) {
const versionID = response.data.map(function(version) {
const vId = version.id;
return vId;
});
return axios.all([
axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers),
axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=status IN (Reported) AND project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers),
axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=status IN (Confirmed) AND project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers))]);
}).then(axios.spread(function(response1, response2, response3) { ...
})
.catch(err => {
console.error(err, err.stack);
});
您将结果链接为常规承诺。您在第一次调用中返回下一个axios调用,然后获得响应。
答案 2 :(得分:0)
实现此目的的一种方法是只调用第一个then
的{{1}}并使用模板字符串。
像这样:
GET
或者,您可以使用const getMyStuff = new Promise((resolve, reject) => {
axios.get('https://jira.example.co.uk/rest/api/2/project/PAD/versions', headers)
.then((response) => {
const versionID = response.data.map(({ id }) => id);
axios.all([
axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers),
axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=status IN (Reported) AND project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers),
axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=status IN (Confirmed) AND project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers),
])
.then(resolve)
.catch(reject)
})
.catch(reject);
});
getMyStuff()
.then((...args) => console.log(args))
.catch((err) => console.error(err));
将其清理一下。
为此,我想推荐你this video by MPJ探讨async/await
的基本概念。